Java Foundation-JVM Heap and Stack

Source: Internet
Author: User

Java Memory Partitioning
The runtime data area is Java memory, and the data area to store more things, if the memory area is not divided into management, it will appear to be more disorganized. Programs like regular things that hate clutter.

Depending on the data stored, Java memory is typically divided into 5 regions: program Count Register, local method stack (Native stack), method area (Methon areas), stack (stack), heap .

Program Count Register

Also called the program register. The JVM supports multiple threads running concurrently, and when each new thread is created, it will get its own PC Register (program counter). If the thread is executing a Java method (non-native), then the value of the PC register will always point to the next instruction to be executed, and if the method is native, the value of the program counter register will not be defined. The JVM's program counter register is wide enough to hold a return address or native pointer.

Stacks (Stack):

Also called the stack. the JVM allocates a stack for each newly created thread . That is to say, for a Java program, it runs through the operation of the stack to complete. The stack holds the state of the thread in frames.

The JVM performs only two operations on the stack: stack and stack operations in frames . We know that the method that a thread is executing is called the current method of this thread. We may not know that the frame used by the current method is called the current frame. When a thread activates a Java method, the JVM presses a new frame into the Java stack of threads, which naturally becomes the current frame. During this method execution, this frame is used to hold parameters, local variables, intermediate calculation procedures, and other data.

From this allocation mechanism in Java, the stack can be understood as: Stack is the storage area that the operating system establishes for a process or thread (a thread in a multithreaded operating system) for this thread, which has an advanced post-out feature .

Its related setting parameters:

-XSS--Set the maximum value of the method stack

Local method Stack (Native stack):

Stores the invocation state of the local method.

Method Area:

When a virtual machine loads a class file, it parses the type information from the binary data contained in the class file, and then puts the type information (including class information, constants, static variables, and so on) into the method area, which is shared by all threads, as shown in. There is a special area of memory in the local method area called Chang (Constant Pool), which is closely related to the parsing of string types.

Heap:

The Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine. The Java heap is an area of memory that is shared by all threads. The only purpose in this area is to hold object instances where almost all object instances are allocated memory, but the object's reference is allocated on the stack.

For example, executing string s = new String ("s") requires allocating memory from two places: allocating memory to a string object in the heap, allocating memory in the stack for a reference (the memory address of the heap object, that is, the pointer) , as shown in.

The Java Virtual machine has a directive that allocates new objects in the heap, but does not have instructions to free up memory, just as you cannot explicitly release an object in the Java code area. The virtual machine itself is responsible for deciding how and when to release the memory occupied by objects that are no longer referenced by the running program, and typically the virtual machine gives the task to the garbage collector (garbage Collection). Its related setting parameters:

-XMS-- Set heap memory Initial size -xmx-- set heap memory maximum -xx:maxtenuringthreshold-- sets the number of times the object survives in the Cenozoic-xx: Pretenuresizethreshold--Set large objects larger than the specified size to be allocated directly in the old generation

The Java heap is the primary area of garbage collector management and is therefore referred to as the GC heap (garbage collectioned heap). Now the garbage collector is basically a generational collection algorithm , so the Java heap can also be subdivided into: the New Generation (young Generation) and the older generation (old Generation), as shown in. The idea of generational collection algorithms: The first is to scan and recycle young generation with a higher frequency, which is called minor collection, and the old generation is much less frequently checked and recycled. Called Major collection. This does not require each GC to check all the objects in memory, in order to make more system resources available for the system to use, another way of saying that when the allocated object encountered low memory, the new generation of GC (young GC), when the new generation of GC is still unable to meet the memory space allocation requirements, The entire heap space and the method area are GC (full GC).

There may be a question for readers here: remember what a permanent generation (Permanent Generation) is, does it not belong to the Java heap? Kiss, you got the right answer! In fact, the legend of the permanent generation is the above-mentioned method area, which is stored in the JVM initialization when the loader loaded some types of information (including class information, constants, static variables, etc.), the life cycle of this information is longer, GC will not be in the main program run time to PermGen space cleanup, So if you have a lot of classes in your application, you're likely to have permgen space errors. Its related setting parameters:

-xx:permsize-- set the initial size of the perm area -xx:maxpermsize--Set the maximum value of the perm area

New generation (young Generation also divided into: Eden and Survivor District, the survivor area is divided into from space and to space. The Eden area is where the object was originally assigned; By default, the area of the from space and to space are equal in size. When the JVM performs a minor GC, it copies objects that are still alive in Eden to the Survivor area, and copies objects that are still alive in the survivor area to the tenured area. In this GC mode, the JVM differentiates survivor from space and to space in order to increase GC efficiency, which separates object reclamation from object promotion. The Cenozoic size setting has 2 related parameters:

    • -XMN--Set the Cenozoic memory size.

    • -xx:survivorratio--Set the size ratio of Eden to survivor space

old Generation: when there is not enough space in older, the JVM will be major collection in the elderly area, and after a complete garbage collection, if the survivor and old areas still cannot store some objects copied from Eden, the "Out of memory error" occurs when the JVM is unable to create an area for the new object in the Eden area.

Java Foundation-JVM Heap and Stack

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.