JVM Memory Management (II)

Source: Internet
Author: User

JVM Memory Management

  During the execution of a Java program, the JVM divides the memory into several different data regions. These zones have their own purpose, as well as the creation and destruction of time, and some regions exist as the virtual machine process starts, and some regions depend on the user thread's start and end to build and destroy.

Program counter

  Program counter: The line number indicator of the byte code executed by the current thread.

  Because the multithreading of the JVM is implemented in a way that threads take turns switching and allocating processor execution time, at any given moment, a processor executes only the instructions in one thread. In order to be able to return to the correct execution location after the thread switch, each thread needs a separate program counter, the counters between the threads do not affect each other, independent storage.

Program counter: Thread-private memory.

Java virtual Machine stack

 Each method executes, creating a stack frame that stores information such as local variable tables, operand stacks, dynamic links, method exits, and so on.

Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.

The memory space required for a local variable is allocated during compilation, and when entering a method, the method needs to allocate much of the local variable space in the frame is fully deterministic and does not change the size of the local variable table while the method is running.

The Java virtual machine stack, like the program counter, is thread-private with the same life cycle as the thread.

Local method Stack

The local method stack is similar to a virtual machine stack, except that the virtual machine stack executes Java method services for the virtual machine .

The local method stack executes the Native method service for the virtual machine .

Java 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 the Java heap is to hold object instances where almost all of the object instances are allocated memory.

That is: All object instances and arrays are allocated memory in the heap.

The Java heap is divided into Cenozoic and old generation.

The Cenozoic is divided into:Eden area,fromSurvivor District,toSurvivor District. At present, the collector basically uses the Generational collection algorithm, further divides the memory space The goal, is in order to better reclaim the memory or the faster allocates the memory.

Method area

The method area is used to store data such as class information, constants, static variables, and timely compiler-compiled code.

Garbage collector

 Program counter, virtual machine stack, local method stack three regions are born with threads, and are extinguished with threads.

stack frames in the stack are methodically executed into and out of the stack as the method enters and exits. How much memory is allocated in each stack frame is basically known when the class structure is determined.

The memory allocations and recoveries in these areas are deterministic, and in these regions there is no need to consider the issue of recycling too much. Because the method ends or the thread ends, the memory is naturally recycled.

Memory allocation

The memory allocation of the object, in the general direction, is allocated on the heap.

The object is primarily allocated on the new generation of Eden , and if a local thread allocation buffer is started, it will be allocated on Tlab by thread precedence . In rare cases, it may also be distributed directly in the old age.

  New Generation GC (Minor GC): Refers to the garbage collection action that occurs in the new generation, because most java objects have features that are facing out.

Therefore,Minor GC is very frequent, the general recovery speed is also relatively fast.

The old age GC (Major GC): Refers to the GCthat occurred in the old age , Major GCappears, often accompanied at least once Minor GC.

Major GC speeds are generally more than ten times slower than the Minor GC .

Generational collection algorithm:

In the Cenozoic, each garbage collection, found that a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects copy cost, you can complete the collection.

In older generations, because the object has a high survival rate and there is no additional space to guarantee it, it must be recycled using the "Mark -and- sweep", "Mark - sweep" algorithm.

JVM Memory Management (II)

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.