Java Virtual machine memory model

Source: Internet
Author: User
Tags switches java se

If you want to understand how Java garbage collection works, it is important to understand the JVM's memory model. Today we'll take a look at the different parts of JVM memory and if you monitor and implement garbage collection tuning.

1. Stop the World Event

All garbage collection is a "blocking" event ("Stop the World" events), because all application threads must stop until the garbage collection operation is complete before continuing.

Because the young generation always holds short-lived objects, minimizing the GC is very fast and the application is not affected. However, the large GC (Major GC) takes a long time because it checks for all surviving objects. The large GC will make your application unresponsive during garbage collection and should be maximized to reduce this type of GC. If you have an instant response application and there is always a lot of large GC in the execution. You will find that there is a timeout error.

The time that the garbage collector sweeps the rows depends on the policy used by the GC. That's why GC monitoring and tuning is necessary for those high-sounding applications.

2. JVM Memory Model

As you can see, theJVM memory may be divided into different parts, and in a broad sense, theJVM heap memory can be divided into two parts: the younger generation and the older generation (young Generation and old Generation)

3. Younger generation (young Generation)

The young generation is used to store objects generated by new. Garbage collection executes when the young generation space is full. This garbage collection we call the minimization of garbage collection (Minor GC). The young generation can also be divided into three divisions: a Eden Memory Area (Eden Memories) and two Survivor memory areas (Survivor memories).

Focus on the young generation:

    • Most newly created objects are stored in the Eden memory area.
    • When the Eden area is full, the minimized GC is executed, and all surviving objects are moved to one of the survivor areas.
    • Minimizing the GC also examines the surviving objects and moves them to another survivor area, so that at some time, one of the survivor areas is always empty.
    • After multiple GC passes, the surviving object will be moved to the old Generation, which usually sets a threshold for how long the younger generation transforms into the older age.

4, Old Generation

Older generations memory contains objects that have survived by minimizing the GC several times. Garbage collection is usually performed when the old age is full. We call it a large GC (Major GC), which usually takes a long time.

5. Permanent generation (Permanent Generation)

A permanent or persistent generation contains metadata that the JVM uses to describe the classes and methods used in the application. Note The permanent generation is not part of the Java heap memory.

The JVM is stored in a permanent generation during runtime based on the classes used by the application, and also includes methods for the Java SE Library classes. Objects in a permanent generation are garbage collected through a full GC.

Methods Area method

A method area is part of a permanent generation that stores the class structure (constant and static variables at run time) and the code for methods and constructs.

run a constant-rate pool Runtime Constant Pool

Running a constant pool is the representation of a const pool in a class at compile time, which contains the class's run-times, static methods, which are part of the method area.

Java stack memory Java stack memories

Java stack memory is used to execute threads. Contains the specific values of the method's short-lived survival and references to other objects in the heap that are involved in the method.

Java Heap Ram switches (Java heap memory switches)

Provides a large number of memory switch, which we can use to set the memory size and their ratios. One of the commonly used memory switches is as follows.

VM Switch VM Switch Description
-xms Sets the heap initial value when the JVM starts.
-xmx Set the maximum value of a heap
-xmn Set the size of the young generation
-xx:permgen To set the initial value of the permanent generation memory
-xx:maxpermgen Set maximum value for permanent memory
-xx:survivorratio Used to provide the proportions of the Eden and survivor areas, for example, if the young generation's size is 10M,VM switch is-xx:survivorratio=2 then 5 M is assigned to the Eden Zone, and each survivor area is 2.5 M. The default scale size is 8.
-xx:newratio The proportion of the old age/Cenozoic. The default value is 2.

In most cases, the above options are very useful. If you want to try other parameters you can view the official JVM homepage

6. JAVA GC

A Java GC is a process that marks from memory, moves away from the available objects, and frees the allocated space for subsequent creation of the pair of images. One of the biggest features of the Java programming language is automatic garbage collection. Unlike other C-like programming languages, their memory allocation and recycling can be manipulated artificially.

A GC is a background execution that checks all objects in memory and finds objects that are not referenced by the task program. All of these unreferenced objects will be deleted, and the space will be reclaimed for allocation to other objects.

There are two questions about tagging and deleting methods:

1. Because the newly created object will become unavailable, all efficiency is not very high.

2. The objects available after multiple GC are still available after the subsequent GC.

The disadvantage of the above approach is that the Java GC is generational, in the heap memory of the younger generation and the old age. I have explained earlier that based on the minimization of GC (Minor GC) and large GC (Major GC) objects are scanned and moved from one partition to another.

Java GC's type

In the application we can use the following five types of GC. We can use the JVM-related settings to turn on the GC policy for the application, and now we'll look at it in detail.

    The
    1. serial gc  (Serial GC -XX:+USESERIALGC):  serial GC provides a simple tag-purge-compress method for the young generation and the old age GC. Serial GC is very effective on a small number of CPUs running on a standalone application. Very advantageous for applications with low memory footprint. The majority of implementations in the
    2. parallel gc  (Parallel GC -XX:+USEPARALLELGC):  parallel GC are the same as the serial GC, where n is the number of system CPU cores, except for the use of n threads for garbage collection by young generations. We can control the number of threads through the JVM's  -xx:parallelgcthreads=n option. The parallel garbage collector is also known as the parallel collector (throughput collector) because it uses multiple CPUs to speed up the implementation of the GC. Parallel GC uses a single thread to handle garbage collection in the old age.
    3. parallel Old GC (Parallel-GC  -XX:+USEPARALLELOLDGC): This type of GC uses multithreading for both the young generation and the older generations of GC. The other is the same as the parallel GC, the
    4. concurrency token purge collector (Concurrent Mark Sweep (CMS) Collector) (-XX:+USECONCMARKSWEEPGC): The CMS collector is a concurrent short pause collector. It is garbage collected for the old age. The CMS collector shortens the pause time due to GC by performing garbage collection concurrently with the application thread. The CMS collector uses the same algorithm as the parallel collector in younger generations. This type of garbage collector is suitable for unresponsive programs that cannot tolerate prolonged pauses. We can set the number of threads in the CMS collector through  -xx:parallelcmsthreads=n in the JVM. The-XX:+USEG1GC garbage collector can be used in the
    5. G1 garbage collector G1 Garbage Collector (G1) Java 7. Its ultimate goal is to replace the CMS collector. The G1 collector is a parallel, concurrent, incremental compression, low-paused garbage collector.

Unlike other collectors, the G1 garbage collector does not have a young generation, older generation space, which divides the heap space into multiple heap sizes of the same size (heap regions), and when a garbage collection is called, it first collects data from areas that are not very high in real time and is therefore called "garbage."

Transferred from: http://www.cnblogs.com/tonyspark/p/3731696.html

Java Virtual machine memory model

Related Article

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.