Java-related: abnormal features

Source: Internet
Author: User
Java-related: abnormal features

Alex Shipping
"When an exception is raised, most developers must first say that they are expensive. If you continue to ask why they are expensive, the most common answer is that we need to capture the current status of the exception stack. Although this is a large part of the overhead, by listing the characteristics of exceptions, we can know that this is just the beginning of the story. The following are some abnormal features:

Can be thrown.
Can be captured.
Can beProgram.
Can be created by JVM.
Is expressed as the first level object.
The inheritance depth starts from 3.
String (and stacktraceelements from 1.4.
Depends on the local method fillinstacktrace ().
The main difference between exceptions and other objects is that exceptions can be thrown and captured. Let's start our research from the process of investigating the event triggered when an exception is thrown.

Overhead for Exception Handling
To throw an exception, JVM sends the athrow bytecode command. The athrow command causes JVM to pop up the execution stack of the exception object. Then the JVM searches for the current execution stack frame to find the first catch clause. This clause can handle an exception of this class or an exception of its superclass. If no catch block is found in the current stack frame, the current stack frame is released, and an exception is thrown again in the context of the next stack frame, wait until the stack frame containing the matched catch clause is found, or it reaches the bottom of the execution stack. Finally, if no catch block is found, all stack frames will be released and the thread will be terminated after the threadgroup object has the opportunity to handle exceptions (refer to threadgroup. uncaughtexception ). If an appropriate catch block is found, the program counter will be reset to that block.CodeThe first line.

From this description, we can understand that it is very expensive to handle an exception thrown. Let's take a look at the exception feature list above. Note that, apart from the JVM's ability to "instinctively" create an exception, the remaining overhead is no different from the overhead caused by any other first-level object's lifecycle.

Overhead of the first-level object
Recall now. An exception is thrown only when the forced conversion fails. How does JVM handle it? When an application requires forced type conversion, a checkcast operation is triggered. This operation is only used to ensure that the parameter type at the top of the stack is expected. If it is not expected, it will throw a classcastexception.

The elegant method of type check is to use the instanceof operator. One of the differences between checkcast and instanceof is that the latter keeps 0 or 1 at the top of the stack to make the table fail or succeed.

The instanceof operator follows a very strict set of rules to determine whether the operation succeeds or fails. No matter whether a variable reference is null, an array, an interface, or a class, you must pay attention to the rules. Once the variable type is determined, you must search for the level of qualified operands on the other side until the matching type is found or the end of the level is reached. In the case of arrays, the types of basic elements must also be reviewed.

In addition to this overhead, once instanceof is used, you need to forcibly convert the object type in the code that follows. The execution of subsequent type conversion will result in the execution of the checkcast bytecode. Therefore, the logic used to determine whether type conversion works may be repeated (assuming that the JVM does not optimize additional checks ). However, because the instanceof operator does not need to be used to create new objects, It is much cheaper to use memory resources and execution resources than to create and handle exceptions. Then the answer to the first question is obvious, right?

Hidden risks
Capturing classcastexception also has a hidden risk, that is, we may capture the classcastexception thrown from the code that replaces the "doing so" annotation. If the code throws classcastexception in the middle of some operations, we can capture it. If it comes from forced type conversion to myspecialclass and is ignored, the application may be in an inconsistent state.

Dynamic Optimization
So far, all we have done is to evaluate the overhead of each one-time execution in the two encoding styles mentioned. Now we need to know the conditions that the Code will be executed to determine which encoding style should be used.

Consider iteration of a set to understand the actual overhead. If the set contains a homogeneous set of objects and an instanceof operation is performed on each object, the process imposes unnecessary overhead throughout the running time. On the other hand, if the set contains a group of objects of different types, we will use the instanceof operation better without causing a strong exception overhead.

This gives us the final clue of the optimal method: exceptions should be retained for exceptions. It is ideal for performance to use exceptions in case of exceptions; it is also ideal for performance to use checks without exceptions to avoid throwing exceptions.

conclusion
from all of this, we can see that following the optimal method (here, exceptions should be used in the case of exceptions) can produce better performance. Sometimes we need to fully consider the situations similar to the ones described in the Article to determine what the optimal method is, and what the optimal method considers in terms of performance and its optional solutions. But now you don't have to worry about optimizing the code too early. We end with the best coding practices. It has an appropriate standalone performance and provides the optimized performance-both of which are the best.
it is reproduced on the IBM China website and has been deleted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.