Introduction to the JVM memory model

Source: Internet
Author: User

First, what is the JVM

The JVM full name is Java Virtual machine, Java VM.

All Java programs run in the Java Virtual machine and exist in memory.

II. process of implementation

1. Loading: binary bytecode and loaded into the JVM

2, Link: Byte code check, parsing interface, class. Initializes a static variable assignment. Verifying the existence of a property or method

3. Initialization: Static code block, constructor, static property

Third, JVM memory model

Register : The PC Register is the JVM directive that is used to store the next execution of each thread

method Extents : Class information (name, modifier, and so on), static variables in a class, constants defined as final in a class, field information in a class, method information in a class. The method area is also shared globally.

Local Method Stack : This area is used to store the state of each native method call.

JVM Stacks : Thread-Private, base-type (int,double), Object-Address (point-to-heap), each method is a stack frame.

A native method is a Java interface that calls non-Java code. A native method is a Java approach: the implementation of this method is implemented by non-Java languages, such as C or C + +.

Four, class loading mechanism

  Parent delegation mechanism . When the ClassLoader receives a request to load a class, it first delegates the load task to the parent class loader, recursively,

If the parent class loader can complete the class load task, it returns successfully, but only if the parent ClassLoader cannot complete the load task.

Five, heap memory analysis

  

the JVM divides the memory into:

New (Young Generation)

Tenured (old generation)

Permanent generation (Perm)

Vi. garbage collection mechanism

1. Reference count (reference counting)
Principle: This object has a reference, then +1; Deletes a reference, then-1. Only objects with a collection count of 0 are used.

2. Copy
Principle: Divide the memory space into 2 equal regions, using only one region at a time. During garbage collection, traverse the current area of use and copy the objects being used to another area.
Pros: There are no fragmentation issues.
Cons: 1, pause the entire application. 2, need twice times the memory space.


3. Mark-Sweep
Principle: For a "Live" object, it must be traced back to its existence in the stack, static storage of the reference. This reference chain may pass through several object hierarchies. Phase one: Start by traversing all references from GC roots, marking a live object. Second stage: The heap is traversed, and the unmarked objects are purged. This solves the problem of circular references.
Cons: 1, suspend the entire application, 2, will produce memory fragmentation.

4. Marking-Compression
Principle: The first stage marks the living object, and the second stage compresses the tagged object into one of the heaps, in order.
Advantages: 1, avoid the marking of scanning debris problem; 2. Avoid the space problem of copying.

5. Sub-generational
Principle: The garbage collection algorithm based on the object life cycle analysis. The objects are divided into young generation, old generation, and enduring generations, and the different life cycles are recycled using different algorithms (one of the 2-3 methods, 4 adaptive).

(1) minor GC

(2) Full GC

1, full GC will be collected at the same time the young generation, the old generation

2, when the permanent full GC will also be triggered, will cause the class, method meta information unloading

Seven, memory tuning

  some of the phenomena before the system crash :

1, each time the garbage collection more and more long, from the previous 10ms extended to about 50ms, FULLGC time also has the previous 0.5s extended to 4, 5s

2, FULLGC more and more times, the most frequent lapse of less than 1 minutes to do a FULLGC

3, the old generation of memory is getting bigger and the old generation No memory is released after each FULLGC

Q: Why is the garbage collection time getting longer before the crash?

A: According to the memory model and garbage collection algorithm, garbage collection is divided into two parts: memory tag, clear (copy), Mark part as long as the memory size fixed time is constant, the change is the copy part, because each garbage collection has some recovery memory, so increase the amount of replication, resulting in longer time. Therefore, garbage collection time can also be used as a basis for judging memory leaks.

Q: Why are there more and more full GC times?

A: So the accumulation of memory, gradually exhausted the old generation of memory, resulting in new objects allocated no more space, resulting in frequent garbage collection

Q: Why are older generations taking up more memory?

A: Because the young generation of memory can not be recycled, more and more are copied to the old generation

Memory tuning Principles :

1. Most Java applications do not require GC optimization on the server;

2, most of the Java applications that lead to GC problems, are not because of our parameter setting error, but the code problem;

3, before the application on-line, first consider the machine's JVM parameters set to the optimal (most suitable);

4, reduce the number of objects created;

5, reduce the use of global variables and large objects;

6, GC optimization is the last resort to use the means;

7, in the actual use, the analysis of GC optimization code is much more than the optimization of GC parameters;

purpose of GC optimization :

1. Reduce the number of objects transferred to the old age to a minimum;

2. Reduce the execution time of full GC;

in order to achieve the above purpose, generally, the things you need to do are :

1, reduce the use of global variables and large objects;

2, adjust the size of the new generation to the most appropriate;

3, set the size of the old age is the most suitable;

4, select the appropriate GC collector;

Introduction to the JVM memory model

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.