Deep Java Virtual Machine reading notes (JVM memory principle, exception handling section)

Source: Internet
Author: User
Tags xms

Deep understandingJavaVirtual machines:JVMAdvanced Features and best practicesRead notes (Memory principle, exception handling):

1. JVM Runtime, Memory partitioning:

 

2. Program Counter:

The JVM considers this count as the number of rows that the current thread executes for a byte code, and selects the action statement to be executed based on the value of the counter. This is a thread-private, non-shareable, and cannot be accurately executed by the current thread if the share causes the count to be confusing.

There is no outofmemoryerror in this area.

3. Virtual Machine stack

often speaking of stack memory refers to the virtual machine stack . Every method in Java , from the call until the completion of the process, corresponds to a stack frame in the virtual machine stack into the stack to the process.

If the thread requests a stack depth greater than the virtual machine allows, the stackoverflowerror exception is thrown, if the virtual machine stack can be dynamically extended (most of the Java virtual machines are now dynamically extensible, but Java a fixed-length virtual machine stack is also allowed in the virtual machine specification, and an OutOfMemoryError exception is thrown if the extension fails to request enough memory.

4. Local method Stack

The virtual machine stack is used to execute the Java method, and the local method stack is used to execute the local method.

The exception is thrown in the same way as the virtual machine stack.

5. Heap

is the largest memory, thread-sharing area in the JVM . The only purpose is to store object instances . This is also the main collection area of the garbage collector. Because modern garbage collectors are using generational collection algorithms, the Java Heap is also divided into the new generation and the old age.

Heap memory can be adjusted through the parameter -xmx (JVM 's maximum available memory ) and -xms (JVM Initial memory ) , which occurs if the expansion is not extended to continue. OutOfMemoryError 's error.

6. Method Area

A region of memory-sharing in the JVM used to store class information, constants, static variables,class files . The garbage collector also recycles this part of the area, such as the cleanup of constant pools and the unloading of types, but the results are unsatisfactory.
the outofmemoryerror error will also be thrown when the method area memory is not sufficient.

7. Object Creation

Virtual opportunity to a new directive, first checks whether the parameter of this directive can be positioned in a constant pool to a symbolic reference of a class, and checks whether the class represented by the symbol reference has been loaded, parsed, and initialized. If not, you must first perform the appropriate class loading process, and the load check will divide the memory in the heap.

After the new instruction is completed, the <init> method is immediately called to initialize the object, when a complete object is created.

8. Access to Objects

In Sun 's jdk , a pointer access method is used to store the address of the object directly in the reference .

9. OutOfMemoryError Error

The outofmemoryerror exception of Java heap memory is a common memory overflow anomaly in practical applications. When a Java heap memory overflow occurs, the exception stack information "java.lang.OutOfMemoryError" is followed by a further hint "Java heap Space".

This error message occurs when too many objects are produced. Workaround: Adjust the virtual machine heap parameters (-xmx and -xms).

The Java method area stores class information, constants, static variables, and so on. If a large number of classes are generated, such as an SSH project, because the framework and a large number of jar packages are loaded, the class file is loaded into the memory's method area, which can occur if memory cannot continue to expand. Java.lang.OutOfMemoryError, followed by permgenspace information. The method area size can be limited by -xx:permsize and -xx:maxpermsize .

Ten. StackOverflow Errors

A stack memory overflow in Java, usually due to a stack depth exceeding the limit depth, which causes the problem to occur. It is common to use recursion when you accidentally forget to specify the end of the recursion, resulting in a recursive depth exceeding the limit depth, and a stack memory overflow occurs.

11. Algorithms for judging whether an object is alive

Mainstream JVM VMS do not use reference counting because there is a situation where it is impossible to determine whether or not to survive. For example, there are several objects in the loop, and no other objects refer to them, so that, as a matter of principle, the dead object can be recycled, but according to the reference counting method, they count not 0, that is, they cannot be recycled.

The mainstream commercial JVM is currently using accessibility algorithms . With the principle of the graph, all other nodes that are available from the root node (origin)GC root are still referenced and cannot be recycled. The object node (distance ∞) from which it cannot be reached indicates that the object can be reclaimed. GC ROOT represents a reference to an object in a virtual machine stack or to a constant object and a static object in a method area, or to an object referenced in a jni(Java Local method).

12. References

The reference represents an address stored in data a that points to the starting address of another fast memory, which is a reference .

Strong references: They are not recycled at any time. Universal presence, such as object O=new object ();

Weak references: When the garbage collector is working, the object that the weak reference points to is reclaimed.

13. Recycling Process

When reclaimed, the system executes System.GC (), finds an unreachable object, marks the object as being recycled, and checks to see if the object overrides the Finalize () method, if it is overwritten and the method has not been called before, and then puts him in a queue, lined with long queues. Finalize () recycle. Even if the Finalize() method is entered, there is no guarantee that the method can reclaim the object because the object can correlate other references within the method and complete the rescue. In general,Finalize () consumes high cost per execution, but does not guarantee that objects are recycled, so the effect is not good and it is not recommended to call the Finalize () method manually.

14. Garbage Collection Algorithm

Mainstream JVMs use generational collection algorithms (divide and conquer the Java heap):


Cenozoic: A large number of object deaths, small survival, using a replication algorithm

Java Heap Partitioning

old age: A small number of deaths, large numbers of survivors, using marker finishing or labeling removal method

Copy algorithm: Divides the memory into two areas a and B, each time only occupies part a . So that a puts all the objects,B is empty. Mark the surviving part of a at each collection, copy to B , and then unify the contents of a . Cons: High memory footprint! Application: New generation.

Tag cleanup algorithm: Mark all junk objects and clear them uniformly after the tag is complete. Disadvantages: Low efficiency, more fragmentation. Application: old age.

Tag Grooming algorithm: Tag all garbage objects, move the surviving object to one side, and clear the garbage object on the other side. Application: old age.

Deep Java Virtual Machine reading notes (JVM memory principle, exception handling section)

Related Article

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.