JAVA-JVM memory model and garbage collection algorithm "one"

Source: Internet
Author: User

Original address: http://blog.csdn.net/kingofworld/article/details/17718587

First, JVM memory model overall architecture diagram

Run-time Data area

The JVM defines different run-time data areas, which are used to execute the application. Some regions start and destroy with the JVM, and some of the data in some areas is thread independent, as threads are created and destroyed.

The Java Virtual Machine defines various run-time data areas that is used during execution of a program. Some of these data areas is created on Java Virtual machine start-up and is destroyed only when the Java Virtua L Machine exits. Other data areas is per thread. Per-thread data areas was created when a thread was created and destroyed when the thread exits.

1. Procedure counter (the program Counter Register)
Multiple threads, when the number of threads exceeds the number of CPUs or the number of CPU cores, the threads will be polled to seize CPU time resources based on the time slice. Therefore, each line threads to have a separate program counter, recording the next command to run. A thread-private area of memory. If you are executing a Java method, the counter records the Java bytecode address being executed, and if you are executing the native method, the counter is empty.

If that method isn't native, the PC register contains the address of the Java Virtual machine instruction currently being Executed. If the method currently being executed by the thread was native, the value of the Java Virtual machine's PC Register is und efined. The Java Virtual machine's PC register is wide enough-to-hold a returnaddress or a native-pointer on the specific platform .
2. VM Stack (Java virtual machine Stacks)
Thread-Private, created at the same time as the thread. Manages the memory model of the Java method execution. Each method executes creates a frame stack to store information such as the method's private variables, operand stacks, dynamic link methods, return values, return addresses, and so on. The size of the stack determines the reach depth of the method call (how many levels are recursive, or how many layers are nested to call other methods, the-XSS parameter can set the virtual machine stack size). The size of the stack can be fixed, or dynamically expanded. If the depth of the stack is fixed, the requested stack depth is greater than the maximum usable depth, then the Stackoverflowerror is thrown, and if the stack is dynamically extensible, but no memory space is available to support the extension, OutOfMemoryError is thrown.

The following exceptional conditions is associated with Java Virtual machine stacks:
If the computation in a thread requires a larger Java virtual machine stack than are permitted, the Java virtual machine th Rows astackoverflowerror.
If Java Virtual Machine stacks can be dynamically expanded, and expansion are attempted but insufficient memory can be made Available to effect the expansion, or if insufficient memory can is made available to create the initial Java Virtual Mac Hine stack for a new thread, the Java Vsan throws anoutofmemoryerror.

The operations placed on the stack are faster than the Java heap, so it is faster to use local variables within the method as much as possible .
Use the Jclasslib tool to view the structure of a class file. For the stack frame structure diagram:

3. Local methods (Native method Stacks)
It is similar to the virtual machine stack, but it is not a Java method, it is a local method, and the local method is implemented in C.

4.JAVA Heap (JVM heap)
Threads are shared, and all object instances and arrays are stored. The main area of garbage collection. Can be divided into Cenozoic and old generation (tenured).
The new generation is used to store objects that have just been created and young objects, and if the object has not been recycled long enough to survive, older objects will be moved into older generations.
The new generation can be further subdivided into Eden, SurvivorSpace0 (S0,from space), SurvivorSpace1 (s1,to space). All the objects that were just created are put into eden,s0 and S1 at least once and survived. If the surviving object persists for a certain period of time, it will enter the old age (tenured).

A Java Virtual Machine Implementation could provide the programmer or the user control over the initial size of the heap, as well as, if the heap can is dynamically expanded or contracted, control over the maximum and minimum heap size.
The following exceptional condition is associated with the heap:
If a computation requires more heap than can is made available by the automatic storage Management system, the Java Virtua L Machine throws an outofmemoryerror.


5. Method area
Thread-shared, which holds metadata information for classes loaded by virtual machines: normal, static, and instant compiler compiled code. also become permanent generations. if the hotspot virtual machine determines that the definition information for a class is not being used, it will also be recycled. The basic conditions for recycling are at least: all instances of the class are recycled, and the ClassLoader that load the class are recycled

second, garbage collection algorithm

Tag-purge algorithm (mark-sweep)
Marks all the available objects from the root node, and the remaining unmarked are garbage objects, performing the purge. However, the reclaimed space is discontinuous.

Replication algorithm (copying) [for new generation ]
Divide the memory into two pieces, use only one piece at a time, and when garbage is collected, copy the tagged object to another piece, and then completely erase the memory that was originally used. The space after the copy is contiguous. The replication algorithm is suitable for the new generation, because the garbage object is more efficient than the surviving object. In the new generation of serial garbage collection algorithms, the objects that are tagged alive in Eden are copied from unused S1, and the young objects in the S0 enter the S1, and if the S1 space is full, the old age is entered, and S0 and S1 are used interchangeably. This improved replication algorithm not only guarantees the continuity of space, but avoids a lot of wasted memory space.

Tag-compression algorithm (mark-compact) [ for the old age]
Suitable for older age algorithms (with more surviving objects than garbage objects).
Instead of copying, the surviving object is compressed to one end of the memory and then all objects outside the bounds are cleaned.

Third, JVM parameters

-xx:+printgcdetails Print Garbage collection information

-XMS is the initial value of the heap area, and the online environment needs to be consistent with the-XMX, otherwise the value of capacity will bounce back and forth
-XMX is the maximum value of the heap area
-XSS (or-SS) thread stack size (refers to a thread's native space) 1.5 is the default size after 1M
-xx:permsize and-xx:maxpermsize Method area (permanent generation) initial size and maximum value (but not local method area)
-xx:newratio old age and Cenozoic ratio
-xx:survivorratio Eden and survivor occupancy ratio. For example, 8 indicates that a survivor area occupies 1/8 of the Eden Memory, or 1/10 of the Cenozoic memory, why not 1/9? Because our new generation has 2 survivor, namely S1 and S22. So survivor is a total of new generation of memory 2/10,eden and Cenozoic accounted for 8/10.
After the GC is-xx:maxheapfreeratio, the heap space is reduced if the free heap memory is found to be less than this value for the entire estimated percentage.
-xx:minheapfreeratio GC, increase heap space if you find that free heap memory is larger than this value for the entire estimate.
-xx:newsize Cenozoic Size

Reference article:

http://www.cubrid.org/blog/dev-platform/understanding-java-garbage-collection/

Http://www.Oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html

http://colobu.com/2015/04/07/minor-gc-vs-major-gc-vs-full-gc/

JAVA-JVM memory model and garbage collection algorithm "one"

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.