- 浏览: 292439 次
文章分类
- 全部博客 (148)
- Shell (2)
- Python (4)
- Java (31)
- Javascript (4)
- Android (7)
- SQL优化 (0)
- Linux (5)
- webOS (4)
- MySQL (18)
- memcach redis (1)
- memcach (0)
- redis (3)
- memcache (2)
- svn (1)
- UED (1)
- 网络 (1)
- virtual box (1)
- git (1)
- Gitosis (1)
- 云计算 (2)
- 12306 (1)
- google (1)
- spdy (1)
- nginx (1)
- tomcat (2)
- SSL (2)
- lucene (2)
- 空间搜索 (1)
- lzo (1)
- 数据压缩 (1)
- ios (1)
- maven (1)
- elasticsearch (1)
- scribe (1)
- flume (1)
- jni (1)
- jna (1)
- hadoop (2)
- 大数据 (1)
最新评论
-
aa80303857:
不错,学习了。互相关注一下。
Sitemesh学习笔记 -
xiaozang:
...
关于nginx的rewrite重写规则 -
coderuncle:
楼主有没有研究过cloudera flume和apache f ...
scribe,flume -
奔跑的犀牛:
...
linux下自动启动mysql -
lsn_1212:
网上装svn的资源太多了,这个挺好的,说的挺全的。
SVN Server安装部署攻略(Linux+SubVersion+Apache)
Recently there was a bigger discussion at dynaTrace around the cost of exceptions. When working with customers we very often find a lot of exceptions they are not aware of. After removing these exceptions, the code runs significantly faster than before. This creates the assumption that using exceptions in your code comes with a significant performance overhead. The implication would be that you better avoid using exceptions. As exceptions are an important construct for handling error situation, avoiding exceptions completely does not seem to be good solution. All in all this was reason enough to have a closer look at the costs of throwing exceptions.
The Experiment
I based my experiment on a simple piece of code that randomly throws an exception. This is not a really scientifically-profound measurement and we also don’t know what the HotSpot compiler does with the code as it runs. Nevertheless it should provide us with some basic insights.
public class ExceptionTest { public long maxLevel = 20; public static void main (String ... args){ ExceptionTest test = new ExceptionTest(); long start = System.currentTimeMillis(); int count = 10000; for (int i= 0; i < count; i++){ try { test.doTest(2, 0); }catch (Exception ex){ // ex.getStackTrace(); } } long diff = System.currentTimeMillis() - start; System.out.println(String.format("Average time for invocation: %1$.5f",((double) diff)/count)); } public void doTest (int i, int level){ if (level < maxLevel){ try { doTest (i, ++level); } catch (Exception ex){ // ex.getStackTrace(); throw new RuntimeException ("UUUPS", ex); } } else { if (i > 1) { throw new RuntimeException("Ups".substring(0, 3)); } } } }
The Result
The result was very interesting. The cost of throwing and catching an exception seems to be rather low. In my sample it was about 0.002ms per Exception. This can more or less be neglected unless you really throw too many exceptions – and too many means we are talking about 100.000 or more.
While these results show that exception handling itself is not affecting code performance, it leaves open the question: what is responsible for the huge performance impact of exceptions? So obviously I was missing something – something important.
After thinking about it again, I realized that I was missing an important part of exception handling. I missed out the part on what you do when exceptions occur. In most cases you – hopefully – do not just catch the exception and that’s it. Normally you try to compensate for the problem and keep the application functioning for your end users. So the point I was missing was the compensation code that is executed for handling an exception. Depending on what this code is doing the performance penalty can become quite significant. In some cases this might mean retrying to connect to a server in other cases it might mean using a default fallback solution that is providing a far less-performing solution.
While this seemed to be a good explanation for the behavior we saw in many scenarios, I thought I am not done yet with the analysis. I had the feeling that there is something else that I was missing here.
Stack Traces
Still curious about this problem I looked into how the situation changes when I collect stack traces. This is what very often happens. You log an exception and its stack trace to try to figure out what the problem is.
I therefore modified my code to now get the stack trace of an exception as well. This changed the situation dramatically. Getting the stack trace of an exception had a 10x higher impact on the performance than just catching and throwing them. So while stack traces help to understand where and possibly also why a problem occurred, they come with a performance penalty.
The impact here is often very high as we are not talking about a single stack trace. In most cases exceptions are thrown – and caught – at multiple levels. Let us look at a simple example of a Web Service client connecting to a server. First there is an exception at the Java library level for the failed connection. Then there is a framework exception for the failed client and then there might be an application-level exception that some business logic invocation failed. This now sums up to three stack traces being collected.
In most cases you should see them in your log files or application output. Writing these potentially long stack traces again comes with some performance impact. At least you normally see and you can react to them if you look at your log files regularly – which is something you do, don’t you?
In some cases I have seen even worse behavior due to some incorrect logging code. Instead of checking whether a certain log level is enabled by calling log.isxxEnabled () first, developers just call logging methods. When this happens, logging code is always executed including getting stack traces of exceptions. As the log level however is set too low they never show up anywhere you might not even be aware of them. Checking for log levels first should be a general rule as it also avoids unnecessary object creation.
Conclusion
Not using exceptions because of their potential performance impact is a bad idea. Exceptions help to provide a uniform way to cope with runtime problems and they help to write clean code. You however need to trace the number of exceptions that are thrown in your code. Although they might be caught they can still have a significant performance impact. In dynaTrace we, by default, track thrown exceptions – and in many cases people are surprised by what is going on in their code and what the performance impact is in resolving them.
While exception usage is good you should avoid capturing too many stack traces. In many cases they are not even necessary to understand the problem – especially if they cover a problem you already expect. The exception message therefore might prove as being enough information. I get enough out of a Connection refused message so I do not need the full stack trace into the internal of the java.net call stack.
发表评论
-
Spring发送邮件。
2013-06-04 15:15 1185Spring邮件抽象层的主要包为org.springfram ... -
JNA—JNI终结者
2013-03-04 14:08 1426JNA— http://blog.csdn.n ... -
Java 压缩实现
2013-01-29 10:51 1034Java压缩技术(一) ZLib Java压缩技术(二) ZI ... -
深入理解JVM内幕:从基本结构到Java 7新特性
2013-01-26 22:57 877原文链接 http://www.importnew.com/ ... -
使用logback轻松管理日志
2013-01-22 14:12 2281最近才开始在项目中使用logback,有一种相见恨晚的感觉, ... -
SSL双向认证java实现
2013-01-09 13:58 1183本文通过模拟场景,介绍SSL双向认证的java实现 默认的情 ... -
TOMCAT-SSL双向认证-配置实例
2013-01-09 13:54 1117SSL (Secure Socket Layer - 安全套接 ... -
Java入门-简单的RMI示例
2012-12-06 11:11 1241RMI是一种分布式技术,使用RMI可以让一个虚拟机上的应 ... -
Java入门-BitSet的使用
2012-12-06 11:01 9491在使用PAT-Tree的中文实现中需要用到大量的位运算, ... -
由12306.cn谈谈网站性能技术
2012-10-17 16:35 96112306.cn网站挂了,被 ... -
Server redirected too many times
2012-09-17 16:54 6047我之前遇到过这种问题,有些网站是这样的,他判断cooki ... -
java并发编程
2012-08-15 19:45 0Queue BlockingQueue Con ... -
在junit中使用open session in view
2012-08-07 11:22 2099废话不多说直接上代码: jpa的 import ... -
mvn指定项目名称
2012-06-07 17:10 982call mvn eclipse:clean eclipse: ... -
在JVM关闭前想做的操作Rumtime
2011-12-01 11:07 1102在虚拟机被关闭前想做一些收尾工作,可以使用 Runtim ... -
Apache和Subversion集成安装与配置
2011-08-12 17:30 1175要准备的东西如下:1,apac ... -
Nginx+tomcat 做负载均衡
2011-08-12 17:27 1384一、 1、将tomcat 的serv ... -
SVN Server安装部署攻略(Linux+SubVersion+Apache)
2011-07-18 18:45 2737一. 安装apr依赖库(Apache Po ... -
git
2011-05-26 11:38 12531. git init 2. cd - 快速返回 3 ... -
java工具集合
2011-05-23 17:29 892code review gerrit g ...
相关推荐
在编程世界中,异常是程序运行时遇到的不正常情况,它们中断了正常的代码执行流程。在JavaScript中,异常处理是通过try...catch语句来实现的,这使得程序员能够优雅地处理错误,而不是让程序突然崩溃。...
通过pip安装better_exceptions : $ pip install better_exceptions 并将BETTER_EXCEPTIONS环境变量设置为任何值: export BETTER_EXCEPTIONS=1 # Linux / OSX setx BETTER_EXCEPTIONS 1 # Windows 而已! ...
Laravel例外Laravel Exceptions由创建并维护,并为开发和生产提供了强大的错误响应系统。 它可以选择将软件包用于开发错误页面。 随时检查,,,,和。安装Laravel异常需要 7.2-8.0。 此特定版本支持Laravel 7-8。 ...
Handling the exceptions: try { something; } catch(Exception e) { StackOverflow.search(e); } Sofa So probably you're tired of copy pasting logcat exceptions and stack traces into search, right? Now ...
《PyPI官网下载:tidevice-0.4.8.tar.gz——探索Python在云原生环境中的应用》 PyPI(Python Package Index)是Python开发者的重要资源库,提供了丰富的Python库供全球开发者下载和使用。本文将围绕“tidevice-...
什么是django-http-exceptions ? 对于您的django视图,这是很可笑的例外。 到底有什么好处呢? 这使得 def some_function (): raise SomeError def view ( request ): try : response = some_function () ...
【标题】"vertx-https-exceptions:https的转载者" 涉及的主要知识点是使用Vert.x框架处理HTTPS通信中的异常。Vert.x是一个轻量级、反应式、非阻塞的Java库,用于构建分布式系统,特别是网络应用。在这个项目中,...
标题中的"Exceptions: JavaScript的基本异常类"指的就是JavaScript中用于表示和处理错误的结构。JavaScript提供了一个内置的异常处理系统,它基于`Error`对象及其子类。这个系统允许开发者在遇到错误时抛出异常,并...
"ocp-ws-exceptions: OCP研讨会-例外"这个标题暗示我们将会深入探讨Java中的异常处理,特别是与Oracle Certified Professional (OCP)相关的部分。OCP是Java开发者认证的一种,它涵盖了广泛的Java知识,包括异常处理...
该应用程序演示了我的MVC Exceptions博客中涵盖的大多数要点: 。 发行历史 2013年11月:V1 2014年10月:V2 2018年4月:V2.0.1 2021年1月:V2.1.0 应用概述 最重要的文件是: 演示1 带有@ExceptionHand
epfl-例外包含epfl软件包的异常用法var exceptions = require ( 'epfl-exceptions' ) ;var ParameterException = exceptions . ParameterException ;var ServerException = exceptions . ServerException ;var ...
EntityFramework.Exceptions 使用Entity Framework Core时,轻松处理数据库错误。 支持SQLServer,PostgreSQL,SQLite和MySql EntityFramework.Exceptions有什么作用? 使用Entity Framework Core进行数据访问时,...
在Java编程语言中,异常处理是一项至关重要的技能,它确保了程序在遇到错误或异常情况时能够优雅地处理问题,而不是突然崩溃。异常是程序执行过程中出现的不正常情况,通常由错误的操作、非法的数据或者不可预见的...
js_obj = execjs.compile(js_code, cwd='node_modules') ``` 但这样做可能会遇到新的问题,例如`TypeError: Cannot read property '0' of null`,这通常表示在JavaScript代码中尝试访问一个`null`对象的属性。要...
retry_on_exceptions 装饰器用于通过捕获指定的异常之一然后重试来重试函数 N 次的装饰器。 对于偶尔抛出错误的函数特别有用,例如依赖外部资源(如 Web API、数据库等)的函数。 安装: pip install retry_on_...
this.statusCode = statusCode; this.name = 'HTTPException'; } } ``` 这样的自定义异常可以帮助我们区分不同类型的HTTP错误,并提供额外的状态码信息。 标签"JavaScript"表明这个话题主要是在JavaScript上下...
Java异常分为两种类型:未检查异常(Unchecked Exceptions)和检查异常(Checked Exceptions)。自定义异常如果继承自`RuntimeException`或其子类,将成为未检查异常,否则默认为检查异常。未检查异常在编译时不强制...
十分方便的一个asp.net的日志管理dll。无需写代码,只要配置一下就可以。 是一款ASP.NET下的系统错误记录管理工具,它可以非常方便的把“黄屏”错误记录到XML,MS SQLServer,SQLite,MySql等文件中,甚至它还可以...
例外情况 该软件包提供了(可选的纯)可扩展异常,这些异常与monad转换器库兼容。 联系信息 欢迎提供贡献和错误报告! 请随时通过github或irc.freenode.net上的#haskell IRC频道与我联系。 爱德华·克梅特...
Cabother例外自定义例外的参考书目努吉特https://www.nuget.org/packages/Cabother.Exceptions/例外清单Nome da Exception Descrição Mensagem doUsuário ConfigurationNotFoundException Solicitado ...