Java garbage Collection-generational collections

Source: Internet
Author: User
Tags compact

Java Automatic garbage collection (Automatic garbage Collection) is an automatically reclaimed memory that is no longer used on the heap , and new objects are recycled if they are not referenced in the program. There are many implementations of recycling, reference counting collector/tracing collector/compacting collector/coping collector/generational Collector /adaptive Collector. This document records the generational Collector (generational collector) used by the HotSpot Java VM.

why generational?

Is the size of the data that survived the garbage collection over time. It can be seen that most objects survive for a short period, with fewer objects surviving over time. Therefore, different recovery frequencies and methods can be taken for different heap memory to improve JVM performance.


Memory Generational concept


Eden: The memory space allocated when the new object is used, most of the initial new objects are in that space

Survivor space: After a garbage collection in Eden, the surviving objects are stored in the

Tenured space: objects that exist for a period of time in survivor spaces are moved

Permanent SPACE:JVM uses metadata, such as ClassLoader-loaded Class/method definitions (reflected data).

Code Cache: Used to compile and store original codes

Permanent space and code cache are not part of the Generation Collector recovery range.

how to collect in generations

The new object is initially assigned in Eden and fires minor garbage collection when Eden is full.

Minor GC (also known as young GC) For young generation:

    • The surviving objects in the Eden/s0 are copied to S1 and Eden/s0 are emptied.
    • Each time a GC is minor, the age of the surviving object is added to 1, and when age exceeds the threshold, the object is stored in the old generation.
    • S0 and S1 are relative, the first time minor GC s0/s1 are empty, from Eden to S0 replication, the second minor GC is from Eden/s0 to S1 copy, the next is eden/s1 to S0, and so on and so on.
The Minor GC uses copy collection, and most of the objects in young generation are no longer used, so a process that replicates the surviving objects to the new space, and the old space is recycled, is faster.

With the occurrence of minor GC, more and more objects are promoted to old generation, and when old generation is full, major garbage collection is triggered. Major GC (also known as Old GC) takes Marking-deletion-compact (Mark-purge-compress memory) method for old generation. (Complement: Compressed memory, is to put the living object in a piece, save the continuous free memory, easy to allocate)

usually the major GC is also considered full GC, as the major GC occurs with the minor GC: when a minor GC places the surviving object on the old generation, it causes the old generation to fill. This results in major GC. This period of time the whole heap memory shows the phenomenon is all carried out garbage collection.

Recommended

The Minor/major GC is the Stop the World event, and the application stops executing until the GC is complete. Major GC occurs, the entire heap memory is affected and lasts for a long time.

Therefore, you should try to avoid or reduce major GC (reduce the object that is put to old generation), maximize heap memory.

Actual use

when you start a Java program, you can optimize the GC's impact on the program by setting the relevant command parameters to adjust the method, threshold, or frequency of garbage collection, which are common commands:

      Switch Description
      -Xms Sets the initial heap size for when the JVM starts.
      -Xmx Sets the maximum heap size.
      -Xmn Sets the size of the young Generation.
      -xx:permsize Sets the starting size of the Permanent Generation.
      -xx:maxpermsize Sets the maximum size of the Permanent Generation
string parallelism of GC

The serial GC is the default on Javase 5/6, and is often used on applications where the pause time requirement is not high, the client type, and the number of applications that are more than the CPU, so that the GC has a small effect on the JVM. MB-level heap memory takes only a few seconds to perform full GC. The command set is-XX:+USESERIALGC.

Parallel GC uses multithreading for minor GC, the number of threads is equal to the number of CPUs by default. The related commands that are set are-XX:+USEPARALLELGC and-xx:parallelgcthreads=<desired number> (Specify the number of threads). -XX:+USEPARALLELOLDGC will set both minor GC and major GC to use multithreading.

Cms

The concurrent Tag purge collector (Concurrent Mark Sweep) is used for garbage collection of old generation, and it tries to reduce program pauses, GC work, and application threads executing concurrently. It does not make compact (compressed) heap memory, eliminating the copying and movement of memory. When too many memory fragments fail to meet the allocation, a larger heap of memory is redistributed directly. CMS is suitable for webserver or query class applications .

Related commands:-XX:+USECONCMARKSWEEPGC and-xx:parallelcmsthreads=<n> (set number of parallel threads)

G1 Garbage Collector

The G1 collector was introduced in the Java7, hoping to replace the CMS, which is mainly concurrent, incremental compact memory to achieve a low pause. Related commands:-XX:+USEG1GC

An instance

Java-xmx12m-xms3m-xmn1m-xx:permsize=20m-xx:maxpermsize=20m-xx:+useparalleloldgc-jar Java2demo.jar

Reference: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Java garbage Collection-generational collections

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.