JVM Memory Management notes and jvm Memory Management

Source: Internet
Author: User

JVM Memory Management notes and jvm Memory Management

This article summarizes and translates SUN's documents (which have been acquired by Oracle )."Memory Management in the Java HotSpot™Virtual Machine ", Sun Microsystems, rjl 2006"

Take notes on JVM memory management. Most of the illustrations are from documents. The original official link is invalid because of the acquisition. Please refer to the original article through cloud Disk

Http://yun.baidu.com/share/link? Required id = 1375498359 & uk = 1191828943 & third = 0

Oracle's garbage collection documentation in http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/

1. manual and automatic memory management

Manual management of memory, you need to clearly specify when the object will not be used and release it, but automatic management, you need to determine whether to release based on the object reference situation. Of course, the referenced objects are not necessarily useful. The chaotic references cause some objects not to be released. Automatic Management solves most memory problems, but cannot completely solve the memory problems.

2. Concept of garbage collection

The Garbage Collector (GC) is a JVM tool for memory management.

The garbage collector is responsible:

  • Allocate memory
  • Save all referenced objects in memory
  • Reclaim memory occupied by objects that are no longer referenced during Code Execution

The referenced objects (which should be effectively referenced) in Java programs are called live ). objects that are no longer referenced are called dead objects, also known as garbage ). the process of searching for dead objects and releasing memory space is called garbage collection.

2. 1. What is a good garbage collector?

  • Safely and efficiently allocate and release memory space

SecurityThat is, useful data will not be recycled, and useless data will not be released for a long time;EfficientThat is, the recycler will not cause a long lag during operation.

However, most systems have trade-offs between time, space, and frequency. A small heap will increase the recovery speed, but the heap will soon be filled, resulting in more frequent recovery.

  • Fragment restrictions

After an object is released, the recycled space may be scattered in different blocks.FragmentThese fragments may not have enough space to allocate a large object. We need to remove fragments to obtain continuous space, and the method for eliminating fragments becomesCompaction).

  • Scalability

Whether distribution and recycling become the bottleneck for multi-threaded applications to expand on a multi-processor system.

2. What should I weigh against the garbage collection algorithm?

To design or select a garbage collection algorithm, make the following options:

  • Serial and Parallel)

SerialOnly one CPU is recycled,ParallelThe recycling task is divided into different CPUs for simultaneous execution. Although parallel processing is fast, it will produce additional complexity and potential fragments.

  • Synchronization (Concurrent) and pause (Stop-the-world)

"Pause"When the application is recycled, the application is completely suspended.SynchronizationYou can increase the overhead to shorten the pause time to improve performance. synchronization can be performed with the application at the same time to increase the response speed. However, you must be extremely careful when updating objects.

  • Compacting and Non-compacting and Copying)

"Compaction"After the recycle job, all the surviving ones are moved together to completely recycle the remaining memory. Then, point the pointer to the first free position to quickly allocate objects, and then update the free position for the next allocation."Non-dense"After an object is released, it is not processed to quickly complete the recycle task. Therefore, potential fragments are generated.CopyThe surviving objects can be "evacuated" to different memory areas, and the original area can be quickly used for the next allocation, but it requires additional time and space.

3. Four garbage collectors from J2SE 5.0 HotSpot JVM

3.1.HotSpot JMM Overview

HotSpot JMM has three blocks: Young Generation, Old Generation, and Permanent Generation ), most objects are allocated to the Eden Space of the young generation after initialization. When the Eden Space is saturated, It is recycled through the same vor Space, after multiple recycles, the surviving objects will be moved to the Tenured Space on the old generation ). the surviving objects that have been recycled by the young generation are allocated to the old generation. Objects with large space usage can be directly allocated to the old generation. Persistent storage can be directly recognized as persistent objects by the garbage collector, such as classes, methods, and description classes.

. Garbage collection type

When the Young Generation is saturated, the Young Generation recycling (Minor Collection) will be executed ). full Collection is executed when the old or persistent generation is saturated, also known as the Major Collection to recycle all the generation objects. Generally, objects of the young generation are recycled first. To ensure efficiency, the young generation uses specific algorithms for recycling. The old and persistent generations use the same algorithm.

3.3.Serial Collector (SC serial recycler)

The SC is executed by a single thread. The application execution is paused when the application is recycled.

  • SC's young generation recovery mechanism:

During recycling, the objects that survive in the Eden Space of the young generation are copied To the Space in the same vor Spaces. However, if the object size exceeds the Space of the Space, it will be copied directly to the old generation. Objects in the same vor Spaces From Space are still considered To be young and therefore copied To Space until the objects are marked as old. When To Space is full, surviving objects in Eden Space and From Space will not be copied in, no matter how many young generations of garbage collection experience. After all the surviving objects in Eden Space and From Space are copied, all the remaining objects (marked as red X in the figure) are marked as dead and no longer checked and marked. After the young generation is recycled, Eden Space and From Space will be cleared. If there are objects not in the old generation, it can only be in To Space. At this time, From Space and To Space exchange roles.

  • SC's old generation recovery mechanism:

SC uses the mark-sweep-compact Algorithm for garbage collection in the old band.

(1)Mark): Determine which objects are still alive and mark them

(2)Scan (sweeps): Identify spam by scanning several generations

(3)Sliding-compaction): Compress surviving objects to keep them in a continuous spaceSequentialLocation. After the compaction operation, fragments are integrated and concentrated, so that the newly entered objects can be quickly allocated with a concave-convex pointer (bump-the-pointer.

  • Application of SC

Mostly used for client-style. The program can be paused for a short period of time. It is enabled by default in Versions later than J2SE 5.0.

Command configuration:-XX: + UseSerialGC

3.4.Parallel Collector (PC parallel recycler)

As a high-Throughput Collector (Throughput Collector), PC has the core advantages. It is widely used currently. PC is the enhanced version of SC.

  • The young generation recovery mechanism of PC:

In the same way as SC, the PC and SC are still a "pause-Copy" recycler for the young generation. However, parallel processing reduces the time overhead and increases the application throughput.

  • Recovery Mechanism of PC in the old generation:

Like SC, the mark-sweep-compact recycling algorithm is used to improve efficiency, but there are still rare cases where the collection of older generations causes a long pause of the program.

  • PC applications

Server-class. It is enabled by default in Versions later than J2SE 5.0.

Command configuration:-XX: + UseParallelGC

3.5.Parallel Compacting Collector (PCC parallel compression recycler)

Like PC, PCC adopts a new algorithm for garbage collection in the old generation and eventually replaces PC.

  • PCC's young generation recovery mechanism:

PCC and PC use the same method for garbage collection of young generations.

  • PCC's old generation recovery mechanism

The application will be suspended if the PCC's old band is recycled.

(1)Mark): Logically divide the old generation into small and fixed partitions. You can use multiple threads to mark objects that can be directly accessed in the root set and then the remaining objects. As long as there are surviving objects in the area, the size and location information of the area will be collected and updated.

(2)Summary): This method is used to perform operations on the partition, rather than the operation object. In general, when garbage collection is performed in the old generation, due to the compaction operation of the previous generation, the storage density on the left of the current memory space is still very high, even the objects on the left are arranged consecutively. Retrieving sporadic fragments from a high-density area may not be worth the overhead caused by further compaction. Therefore, the sum will check the density of the area from the leftmost end (according to the information collected in the marked area) until it reaches a point, fromThis pointToRightmostThe overhead can be compensated by compaction and recycling the internal fragment space. This point is replaced by the "dense prefix", and the objects on the left side of it are not displaced, and the spaces on the right side are compressed and recycled.

The summary method is actually implemented in serial mode. Of course, it can also be implemented in parallel, but it is not as important as marking and compaction parallel implementation.

(3)Compaction): Based on the summary operation results, multiple threads determine the zone to be filled, and then independently copy the data to fill the zone. Finally, the heap memory allocation is very intensive at one end, and the other end is almost empty.

  • Application of PCC:

Multi-threaded machines, but not suitable for large shared machines.

Specify before the application.

Command configuration:-XX: + UseParallelOldGC

3.6.Concurrent Mark-Sweep (CMS) Collector (CMSC concurrent Mark recycler)

In many application scenarios, fast response time is more important than end-to-end throughput. If a large pile is used, the recycling of the old generation may cause a long pause.

HotSpot JVM introduces CMSC as "low-latency recycler "(Low-latency collector) To solve this problem.

  • CMSC's young generation recovery mechanism:

    CMSC and PC adopt the same method for garbage collection of young generation.

  • CMSC's old generation recovery mechanism:

Most CMSC garbage collection is completed during application execution.

(1)Initial mark): The application is paused. You can mark the root set.Direct AccessTo obtain the initial set of surviving objects.

(2)Concurrent mark): Mark all objects in the initial setIndirect reference.

(3)Remarks/remark): Because the concurrent tag does not pause the application, the updated reference cannot be marked. To this end, the remarks process suspends the application again and marks the modified Objects During Concurrent marking to ensure the integrity of garbage collection. Because the pause Duration Required for the remarks is longer than the pause duration of the initial mark, multithreading is used to improve the efficiency.














(4)Scan): CMSC increases the overhead of the remarks process to reduce the pause duration, and is the only recycler without "compaction" operation, that isNoSave the surviving objectsSequential.

Although non-compaction saves time, the recycler cannot use the concave and convex pointer technology to allocate new objects due to non-random idle memory. To this end, CMSC uses multiple chain queues (fragments should be stored Based on Space size classification) To allocate appropriate positions through chain queues based on the size of new objects. Therefore, compared with other recyclers, CMSC has a higher resource allocation cost in the old generation and requires a larger heap.

In addition, although all objects are cleaned every time in the old age, the subsequent "New garbage" will also be floating garbage ), it can only be cleared in the next old age. Because there is no compaction, fragments will accumulate sooner or later. For this reason, CMSC tracks active objects and splits or merges fragments by estimating space requirements.

  • CMSC application:

CMSC is applicable to applications that require short pause times and can share processor resources with recyclers. It is usually applicable to applications with large datasets in the old age, such as network servers.

Specify before the application.

Command configuration:-XX: + UseConcMarkSweepGC
-XX: + CMSIncrementalMode (incremental mode)

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.