JVM Learning Notes (iii)------memory management and garbage collection.

Source: Internet
Author: User
Tags garbage collection

JVM Memory Composition Structure

The JVM stack consists of heaps, stacks, local method stacks, method areas, and so on, as shown in the following chart:

1) Heap

All the memory of objects created by new is allocated in the heap, and its size can be controlled by-XMX and-XMS. The heap is divided into the Cenozoic and the old generation, and the Cenozoic is further divided into the Eden and survivor areas, and the final survivor is made up of from space and to space, and the structure diagram is as follows:

Cenozoic. New objects are used to allocate memory in the Cenozoic, Eden Space is not enough, the surviving objects will be transferred to the survivor, the Cenozoic size can be controlled by-xmn, you can also use-xx:survivorratio to control the proportions of Eden and survivor old generation. For storing objects that are still alive after multiple garbage collection in the Cenozoic

2) stack

Each thread executes each method by applying a stack frame in the stack, each of which includes a local variable area and an operand stack to hold temporary variables, parameters, and intermediate results during the method call.

3) Local Method stack

Used to support the execution of the native method, which stores the state of each native method call

4) Method Area

Contains the class information to load, static variables, constants of the final type, properties, and method information. The JVM uses a durable generation (permanet Generation) to store the method area, which can be specified by-xx:permsize and-xx:maxpermsize to specify minimum and maximum values

garbage collection Mechanism

The JVM uses different garbage collection mechanisms for the Cenozoic and the old generation respectively

New generation of GC:

The new generation usually has a shorter survival time, so the so-called copying algorithm, based on the copying algorithm, scans the surviving object and copies it into a completely unused space, corresponding to the Cenozoic, which is copied between Eden and from space or to space. The Cenozoic uses a free pointer to control the GC trigger, the pointer keeps the last allocated object in the Cenozoic interval, and when a new object is allocated memory, it is used to check if the space is sufficient and not enough to trigger the GC. When objects are continuously allocated, the objects gradually go from Eden to Survivor, and finally to the old generation,

Using Java VISUALVM to view, can obviously observe the new generation full after the object will be transferred to the old generation, and then empty continue to load, when the old generation is full, will be reported OutOfMemory exception, as shown in the following figure:

The JVM provides a serial GC (Serial GC), a parallel reclaim GC (Parallel scavenge), and a parallel GC (PARNEW) on the execution mechanism

1) Serial GC

The entire scanning and copying process is a single-threaded way, suitable for single CPU, the new generation of small space and the demand for pause time is not very high application, is the client level of the default GC mode, can be-XX:+USESERIALGC to enforce the specified

2) Parallel Recovery GC

In the entire scanning and replication process in a multi-threaded way, for multi-CPU, the time required for a short pause on the application, the server level is the default use of GC mode, can be-XX:+USEPARALLELGC to enforce the designation, with-XX: Parallelgcthreads=4 to specify the number of threads

3) Parallel GC

Use with concurrent GC for legacy generations

GC for old generation:

The old generation and the new generation, the object survival time is longer, more stable, so the mark (Mark) algorithm for recycling, so-called Mark is to scan out the surviving objects, and then to reclaim unmarked objects, after recycling the empty space is either merged, or marked out for the next allocation, The bottom line is to reduce the loss of efficiency caused by memory fragmentation. The JVM provides a serial GC (Serial MSC), parallel GC (parallel MSC), and concurrent GC (CMS) on the execution mechanism, and the details of the algorithm need to be further studied.

The various GC mechanisms above need to be combined, as specified in the following table:

Specify the way

Cenozoic gc mode

Old generation GC method

P>-XX:+USESERIALGC

Serial gc

Serial gc

-xx:+useparalle LGC

Parallel recycle GC

Parallel gc

-XX:+USECONEMARKSWEEPGC

Parallel gc

Concurrent GC

-XX:+USEPARNEWGC

Parallel GC

Serial gc

-XX:+USEPARALLELOLDGC

Parallel recycle GC

/td>

Parallel gc

-xx:+ useconemarksweepgc

-XX:+USEPARNEWGC

Serial GC

Concurrent GC

Unsupported combination

1,-xx:+useparnewgc-xx : +USEPARALLELOLDGC

2,-XX:+USEPARNEWGC-XX:+USESERIALGC

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.