Talk about the garbage collector in a Java virtual machine

Source: Internet
Author: User

Objective

The Java Virtual machine's garbage collector is the scavenger of virtual machine memory, and its presence allows Java developers to devote more effort to business development. Understand the garbage collector, and take advantage of this tool, can better guarantee the stability of the service. This article analyzes the Java Virtual Machine memory model, introduces the common garbage collector algorithms and collector categories, so that the garbage collector configuration and use is no longer remote.

Java Virtual machine memory model

Java Virtual machine memory can be divided into: virtual machine stack, local method Stack, Java heap memory, method area (including running constant pool), program counters, direct memory.

Virtual Machine Stack

The virtual machine stack is thread-private, with the same life cycle as the thread. In other words, once a thread is created, the virtual machine assigns it a separate stack frame to store the thread's local variables, operands, dynamic links, method exits, and so on, and when the thread ends, the stack frame is recycled.

Local method Stack

The local method stack is a stack frame used during the execution of the native method of the virtual machine.

Java Heap Memory

Heap memory is an area shared by all threads that holds object instances and arrays, is the largest chunk of memory, and is the primary area of garbage collection. From the point of view of garbage collection, heap memory is often divided into cenozoic and old age.

Method area

The method area is also an area shared by all threads to store data such as class information, constants, static variables, JIT-compiled code, etc. loaded by the virtual machine. can also become a permanent generation.

Program counter

The program counter is thread-private, and as the line number indicator of the byte code executed by the current thread, each thread has a program counter that records the execution position of the current thread when the CPU switch thread is logged so that the next time it continues to execute from its current position.

Direct Memory

This block is not part of the Java Virtual Machine memory, but is frequently used and can also be called "out-of-heap memory"

Java Virtual Machine garbage collector

Based on the above description of the Java Virtual Machine memory region model, we know that object instances in Java programs are stored in Java heap memory, so garbage collection is primarily for heap memory. In order to better manage the Java object instance, and combine the lifetime of the object instance, the Java virtual machine divides the heap memory into the new generation and the old age, stores the objects that are newly created and the object instances that have survived for a long time, and recycles the memory of the new generation and the old age by the strategy of generational collection.

memory allocation and recovery policy
  • 1, collection of ideas. According to the life cycle characteristics of Java object, virtual machine divides heap memory into Cenozoic and old age, and adopts garbage collection strategy of new generation and old age respectively.
  • 2. The Cenozoic Subdivision is Eden area and two survivor area (i.e. from and to). Most new objects are created frequently and survive for a short time, in order to increase the efficiency of the new generation of garbage collection, the newly created objects are stored in the Eden area, and when the Eden area is nearly full, the virtual machine triggers a minor GC to move the Cenozoic surviving objects to the from zone. The original object in the from area is determined by the age of survival to be placed in the to area or the old age, then the Eden and from areas are emptied, and the to area objects are moved to the from area.
  • 3, large objects directly into the old age, you can configure the maximum value of the new generation of objects, objects beyond this value directly into the old age.
  • 4, before initiating minor GC, will first determine whether the largest available continuous space in the old age is greater than the space occupied by the new generation of objects, if the risk is less than or not allowed to trigger a full GC. Garbage collection algorithm (3 basic algorithms)
  • 1, copy algorithm. For the new generation of garbage collection algorithm. When the new Generation Eden area is nearly full, the Eden object is copied to the from zone, the from zone object is copied to the to area according to the age of survival, then the Eden and from areas are cleared, and then the to area objects are copied to the from zone.
  • 2, Mark-clear algorithm. The garbage collection algorithm marks the objects that need to be recycled, and the tokens are collected directly after they are completed. The garbage collector uses accessibility analysis to determine which objects are alive by setting up a series of GC roots nodes (including stacks, static properties in the method area and objects referenced by constants, and objects referred to in the local method stack), searching down from such nodes, and when the object is not on the GC root reference chain, The description object cannot be reached and can be recycled.
  • 3, marking-sorting algorithm. The garbage collection algorithm marks the objects that need to be reclaimed, moves the surviving object to one end of the memory after the tag is complete, and then cleans the memory outside the end boundary directly. Common garbage collectors

    Because garbage collection in virtual machines is collected in generations, the new generation and the old age garbage collection strategy is not the same, so it is generally used for the new generation and the old age of the garbage collector combination.

  • 1, Serial GC. The new generation collector, using the replication algorithm, is used for new generation garbage collection of client clients and garbage collection for applications with low memory consumption.
  • 2, Serial old GC. Old age collector, using the tag-collation algorithm, for the old age of client clients garbage collection, for less memory-intensive applications for garbage collection.
  • 3, Parallel scavenge GC. The new generation collector, using the replication algorithm, collects the new generation of memory garbage in parallel, can set the garbage collector throughput, and can also set the automatic adaptation adjustment throughput.
  • 4, Parallel New GC. The new generation collector uses the replication algorithm to collect the new generation of memory garbage in parallel.
  • 5, Parallel old GC. Old age collector, using the tag-sorting algorithm, parallel collection of old-age memory garbage.
  • 6, CMS GC. Old age collector, using the tag-clear algorithm, parallel collection of old memory garbage, do not defragment memory. Because business threads are not interrupted during garbage collection, it is easy to produce "floating garbage", which results in full GC. You can trigger a memory grooming task by setting parameters.
  • 7, G1 GC. Instead of differentiating the heap memory from the Cenozoic and older generations, the heap memory is treated as a few evenly divided areas and the most idle memory areas are flagged and reclaimed. For applications with large memory.

Talk about the garbage collector in a Java virtual machine

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.