JVM Memory Knowledge Summary

Source: Internet
Author: User

The main contents of this article:

Http://hllvm.group.iteye.com/group/wiki/3053-JVM

http://my.oschina.net/xishuixixia/blog/133850

http://my.oschina.net/xishuixixia/blog/132395

http://www.cnblogs.com/gw811/archive/2012/10/18/2730117.html#undefined

1. JVM Memory Model:

Program Counter Register (the PC Register)

The program counter registers a small piece of memory, which acts as a line number indicator of the bytecode that the current thread lock executes. At any one time a processor will only execute instructions in one thread, in order to switch threads

Can be restored to the correct execution location, each thread needs a separate program counter. If the method is not native, the program counter register contains the address of the currently executing JVM directive, if the method is native

, the value of the program counter register is not defined (Undefined).

Java Virtual Machine stack

When a thread activates a Java method, the JVM presses a new frame into the Java stack of threads. This frame naturally becomes the current frame. During this method execution, this frame is used to store local variable tables, action stacks, dynamic links,

Method exports and other information. The local variable table holds various basic data types (Boolean, Byte, char, short, int, float, long, double), object references (reference types) that are known during the compilation period. Stack

saves the state of the thread in frames. The JVM performs only two operations on the stack: stack and stack operations in frames. Stack is when the operating system is building a process or a thread (in an operating system that supports multithreading)

The storage area established for this thread, which has an advanced post-out feature.

When nested methods are called, the deeper the nesting, the later the stack's memory is released, so in the actual development process, it is not recommended that you use recursive method calls, recursion can easily lead to stack flow.

When a thread requests a stack that goes deeper than the virtual machine allows, a Stackoverflowerror exception is thrown, and a OutOfMemoryError exception is thrown if enough memory is still not requested after the virtual machine is dynamically extended.

Local method Stack

The local methods Stack (Native method Stacks) is very similar to the virtual machine stack, but the difference is that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, while the local method stack is

The native method service to use for the virtual machine.

Heap

The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this storage area is to hold object instances where almost all object instances and arrays allocate memory.

The Java heap is the main area of garbage collector management, so many times it is called a "gc Heap" (garbage collected heap, fortunately not translated into a "garbage heap" in the country). If from the perspective of memory recycling, because now

Collectors are basically used in the generation of collection algorithms , so the Java heap can also be subdivided into: the new generation and the old age, and more detailed there is Eden space, from Survivor space, to survivor space.

If there is no memory in the heap to complete the instance assignment, and the heap can no longer be expanded , a OutOfMemoryError exception will be thrown.

Method area

The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. Although

The Java Virtual machine specification describes the method area as a logical part of the heap, but it has an alias called Non-heap (Not a heap), and should be distinguished from the Java heap.

The Java Virtual Machine specification has a very loose limit on this area, and you can choose not to implement garbage collection, except that you do not need contiguous memory and can choose a fixed size or extensible, as with the Java heap. Relatively speaking, garbage collection

Behavior is less common in this region, but it is not the data that enters the method area as "permanent" as the name of the permanent generation. The memory recovery targets for this area are mainly for the recovery of constant pools and the unloading of types, generally

In this area, the recovery "performance" is more difficult to satisfy, especially the type of unloading, the conditions are very harsh, but this part of the area of recovery is indeed necessary.

Chang ( run a constant-rate pool? )

The runtime Constant pool is part of the method area. In addition to the class file, such as the version, field, method, interface, and other information, there is a constant pool (Constant

Table), a variety of literal and symbolic references that are used to store compile-time builds , this section is stored in the run-time pool of the method area after the class is loaded. in General, in addition to saving the symbol references described in the class file , you

A direct reference to the translation is also stored in the run-time constant pool. Another important feature of running a constant pool with respect to the class file constants pool is its dynamic nature, and the Java language does not require constants to be generated only at compile time.

It is not the content of the constant pool that is pre-placed in the class file to enter the method area to run the const pool, and the new constants may be put in the pool during the run. Since the run-time constant pool is part of the method area, it is naturally affected by the method area

The OutOfMemoryError exception is thrown when a constant pool cannot be applied to memory again.

Generation of objects

The most common sequence behavior, but even the simplest of accesses, involves the association between the three most important memory areas of the Java stack, the Java heap, and the method area, as in the following code:

          Object obj = new Object ();

Assuming that this code appears in the method body, the semantics of the part "Object obj" will be reflected in the local variable table of the Java stack as a reference type data. And the phrase "new Object ()"

Semantics will be reflected in the Java heap, forming a piece of structured memory that stores all instance data values of type object (Instance data, each instance field in the object), based on the specific type and virtual machine implementation

Object memory layout, the length of this block of memory is not fixed. In addition, the Java heap must also contain data that can be found for this object type, such as Object type, parent class, Real

Address information for existing interfaces, methods, and so on), and these types of data are stored in the method area.

GC in the young generation

The HotSpot JVM divides the young generation into three parts: 1 Eden area and 2 survivor area (called From and to). The default scale is 8:1, so why is this scale by default, and then we'll talk about that. In general, newly created objects are assigned to the Eden area (some large object special handling) that, after the first minor GC, will be moved to the survivor area if it survives. Object in the Survivor area each time minor GC, age will increase by 1 years, when its age to a certain extent, it will be moved to the old generation.

Because the young generation of objects are basically facing to die (more than 80%), so in the young generation garbage collection algorithm uses the replication algorithm, the basic idea of the copy algorithm is to divide the memory into two pieces, each time only one piece, when this piece of memory is used up, will be still alive objects copied to another. The replication algorithm does not produce memory fragmentation.

At the beginning of the GC, the object will only exist in the Eden area and the survivor area named "from", and the Survivor area "to" is empty. All surviving objects in the Gc,eden area are then copied to the "to", and in the "from" area, the surviving objects are determined according to their age values. Objects that reach a certain age (age threshold, which can be set by-xx:maxtenuringthreshold) are moved to older generations, and objects that do not reach the threshold are copied to the "to" area. After this GC, the Eden Zone and the from zone have been emptied. At this time, "from" and "to" will exchange their role, that is, the new "to" is the previous GC "from", The new "from" is the previous GC "to". In any case, the survivor area named to is guaranteed to be empty. The Minor GC repeats this process until the "to" area is filled and the "to" area is filled, and all objects are moved into the old generation.

An object of this lifetime

I'm an ordinary Java object, I was born in Eden, and I saw a little brother like me in the Eden area, and we played in Eden for a long time. One day in the Eden District is really too many people, I was forced to go to the "from" area of the Survivor District, since went to the survivor District, I began to drift, sometimes in the survivor "from" district, sometimes in the survivor "to" district, there is no indefinite. Until I was 18 years old, my father said I am an adult, should go to the society to break through. So I went to the old generation over there, old generation, a lot of people, and age is quite large, I also know a lot of people here. In the old age, I lived for 20 years (each GC plus one year old) and was then recycled.

Tuning parameters that are commonly used.

1. Heap Size

-XMS and-xmx are used to specify the heap size, and we need to set them to the same value to avoid resizing the heap after the GC.

2. Young Generation Size

-xx:newsize= and –xx:maxnewsize=? The young generation size is recommended to be set to a heap size of 1/3 or 1/4, and two values are the same size. Setting the size of the younger generation is quite important, and if the younger generation is set to small, then some objects that can have short life cycles may be moved to older generations, leading to full GC, and setting too large will also cause stop the world.

3. Method Area Size

-xx:permsize=256m and-xx:maxpermsize=256m, the method area size does not affect performance, as long as you set the value to ensure that the application does not report an error such as Oom.

4.GC Log

-XLOGGC: $CATALINA _base/logs/gc.log,-xx:+printgcdetails,-xx:+printgcdatestamps, record as many GC logs as possible, so that you can analyze the system behavior in a comprehensive way.

5.GC algorithm

-XX:+USEPARNEWGC,-xx:+cmsparallelremarkenabled,-XX:+USECONCMARKSWEEPGC,-xx:cmsinitiatingoccupancyfraction= 75, this is just the usual parameters in the general system, you also need to choose the most suitable system according to your system situation.

6. Heap Snapshots

-xx:+heapdumponoutofmemoryerror and-xx:heapdumppath= $CATALINA _base/logs.

7.OOM operations after the occurrence

After oom shutdown server:-xx:onoutofmemoryerror= $CATALINA _home/bin/stop.sh

Restart server after Oom:-xx:onoutofmemoryerror= $CATALINA _home/bin/restart.sh

JVM Memory Knowledge Summary

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.