memory allocation and garbage collection in Java

Source: Internet
Author: User

One, memory allocation

Java Program Runtime memory allocation, according to the JVM specification, including the following areas: program counters, virtual machine stack, local method stack, method area, heap. Of these, the first three are thread-private, the same as the thread life cycle, the thread exits the memory auto-recycle, the two are all threads shared memory, only when the garbage collection mechanism is triggered, passive recycling.

* Program counter, memory area is very small, is the current thread bytecode execution line number indicator;

* The virtual machine stack, the local method stack, which is usually referred to as the "stack", is the virtual machine used to execute methods (including Java, Non-Java methods), the use of temporary memory space to store the current method, local variables, and so on, all basic type variables, as well as the class object references are stored in the stack;

* Method area, global shared zone, used to store class information that has been loaded by the virtual machine, constants (such as string literal constants), static variables, and compiler-compiled code;

* Heap, the largest chunk of memory in Java Virtual Machine management, shared by all threads, used to store all Java class instances. Note that the instance data opens up memory in the heap, and the reference to the object is equivalent to a pointer stored in the stack of threads.

Second, garbage collection algorithm

Three questions to address for garbage collection:

1) which memory can be recycled. In the above areas of memory, only the method area, heap is a global shared memory block, garbage collection needs to be processed, the memory is collected on a timed basis. Determining whether an object is recyclable requires a "accessibility analysis algorithm" to determine which memory blocks are orphaned, unreachable objects, or collections of objects.

2) What time it takes to perform the recycling. When performing a garbage collection, you need to completely "freeze" the scene to reach the so-called "Stop the World" to avoid the problem of changing the reference relationship during GC. The practice of the virtual machine is to let all threads active interrupt, but not immediately force the thread thread, but the GC threads set an identity, so that all other threads to their next "security point", according to this identity and actively suspend the thread, waiting for the operation of the GC thread;

3) How to perform garbage collection. The garbage collection algorithm has many, but the overall strategy is the generational recovery, according to the new generation, the Laosheng generation characteristic, takes the GC algorithm to be different.

* For the new generation of memory blocks, the object survival rate is low, need to clean up frequently, in order to avoid the "mark-clear" algorithm fragmentation problem, but also to improve efficiency, take "copy" algorithm, the new generation (young area) is divided into Eden+survivor1+survivor2 several blocks of space, When the GC is performed, the surviving objects in the Eden/survivor1 are copied into the Survivor2 space and then the entire block is cleared out of the previous Eden+survivor1 area, and the area of memory that is typically 90% is reclaimed.

* For Laosheng memory blocks, the object survival rate is high, there is no need for frequent survival judgment of the scan, when the GC, usually using the "tag-collation" algorithm: Tag can reclaim the region, move the central living object memory, and ultimately the remaining garbage areas to recycle.

Third, garbage collector implementation

When the GC thread is working, the user thread is completely or partially interrupted, affecting the system throughput rate, causing the user task to stall or even die. The new generation of collectors have serial/parnew/parallel scavenge, laosheng generation of collectors have cms/serial old/parallel old.

* Serial/serial old is a single-threaded collector, in the GC, will interrupt all user threads, in the new and Laosheng generation respectively using the copy algorithm and the tag-collation algorithm for garbage cleanup;

* Parnew is a multiple GC thread parallel, but also interrupts all user threads, and the algorithm plane and the serial series are consistent;

* Parallel collector, also is a multi-threaded parallel collector, the difference is that it is concerned about the system throughput rate of the indicator, that is, the user task of the pause time ratio, rather than the absolute time of the pause.

* CMS (Concurrent Mark Sweep) collector, designed to pursue the shortest pause time, is currently the most popular collector implementation. Divided into initial mark/concurrent mark/remark/concurrent sweep four steps. Where initial mark only flags the objects that GC Roots can directly relate to, concurrent mark is a multi-GC thread concurrently with the user thread to perform GC Roots traceing, essentially the growth and tagging of the live object's Unicom domain, The remark phase changes the object reference in the concurrent mark phase when the user thread executes concurrently, and concurrent sweep is to clean up the garbage in parallel. 1th, 3, all user threads will still be interrupted, but because the operation is very simple, so fast, 2nd, 4 steps, time is long, but with the user thread concurrency, so overall, the CMS GC User task pause time is very short.

memory allocation and garbage collection in Java

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.