Home> Java> javaTutorial> body text

How to allocate and manage JVM memory in java?

青灯夜游
Release: 2018-10-22 17:37:44
forward
3745 people have browsed it

The content of this article is to introduce how to allocate and manage JVM memory in java? Let everyone understand the JVM's garbage collection algorithm and JVM's memory allocation mechanism. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Garbage collection algorithm

  • #Memory allocation mechanism in JVM

    The garbage collection algorithm has a mark-sweep algorithm, Mark-collation algorithm and copy algorithm. JVM uses generational collection algorithm to reclaim the memory space allocated by JVM. Generational collection algorithm is divided into new generation and old generation. It mainly collects heap memory in JVM memory model. Among them, new generation uses copy algorithm. , the old generation uses the mark-sort algorithm. Let’s explore the specific ideas of the garbage collection algorithm.

  • Mark-clear algorithm:
    The mark-clear algorithm is divided into two stages: marking and clearing. First, the memory space that needs to be recycled is marked, and after the marking is completed, all marked objects are recycled uniformly. Object. There are two main reasons for it: 1. Its marking and clearing efficiency are not high. 2. It will generate a large number of discontinuous memory fragments after clearing the object space, causing garbage collection to be triggered in advance due to insufficient memory when large objects (objects that require a large amount of continuous memory) are reallocated. Its recycling process is shown in the figure below.

How to allocate and manage JVM memory in java?

  • Copy algorithm
    The copy algorithm divides the memory capacity into two equal blocks, using one block at a time Memory, when this memory is used up, copy the surviving objects in this memory to another memory, then clear this memory space, and allocate the next object to another memory space, that is, there are objects stored The two memory spaces are used alternately. The new generation in the JVM heap memory is divided into Eden space, from survivor space and to survivor space. The default Eden and survivor space ratio is 8:1, of which Eden accounts for 80%, from and to each account for 10%, and the space that can be utilized by all new generation memory is 90%. In the new generation, a large number of objects will be collected during each garbage collection, so only a few surviving objects need to be copied. The following is the operation process of the copy algorithm:

How to allocate and manage JVM memory in java?

  • ##Mark-organizing algorithm:

    The copy algorithm has a relatively high object survival rate. It is not easy to use in high settings because it requires copying a large number of live objects. More importantly, the copy algorithm will waste some space. Because the old generation stores some objects with relatively long lifetimes, it is not suitable to use the replication algorithm. According to the characteristics of the old era, the mark-sort algorithm was produced. The mark-compact algorithm first marks the surviving objects, then moves them to one end, and then releases the object memory outside the end boundary. The execution process of the marking-collation algorithm is as follows:

How to allocate and manage JVM memory in java?

2. Memory allocation and recycling strategyIn the Java technology system The automatic memory management advocated can ultimately be attributed to automatically allocating memory to objects and reclaiming the memory allocated to objects. Regarding memory recovery, you can refer to the garbage collection algorithm above. The JVM uses a generational garbage collection algorithm to recover object memory, which is divided into the new generation and the old generation. The new generation uses the copy algorithm, and the old generation uses the mark-sort algorithm. Let’s take a look at the memory allocation strategy of the JVM:

  • Objects are allocated in the Eden area first

    In most cases, objects are allocated in the new generation Eden area. When the Eden area does not have enough memory space for allocation, the virtual machine will initiate a Minor GC (New Generation GC) to recycle dead objects in the New Generation. Surviving objects are stored in the survivor area. If there is not enough space in the survivor area, Storage will be directly deposited into the old generation through space allocation guarantee. Then store the object in the Eden area.

  • Large objects enter the old generation directly

    Large objects refer to objects that require a large amount of continuous memory space. The most typical large objects are very long strings and very long arrays. . Large objects are bad news for the memory allocation of virtual machines. The frequent occurrence of large objects can easily trigger garbage collection in advance and generate continuous space to store large objects when there is still a lot of space in the memory. The worse situation with small objects is to encounter a group of "short-lived" large objects, which should be avoided when writing programs.

  • Long-term surviving objects enter the old generation
    Since the virtual machine adopts the idea of generational collection to manage memory, the virtual machine needs to know which objects should be placed in the new generation and which objects should be placed in the new generation. Put it in the old age. The virtual machine gives each object an age counter. If this object has experienced a Minor GC in the Eden area and is stored in the survivor area, then the age of this object is 1. Every time it survives a Minor GC, the object's age increases by 1. When its age increases to a certain level ( The default is 15 years old), it will be promoted to the old generation.

  • Dynamic Object Age Determination
    In order to better adapt to the memory conditions of different programs, the virtual machine does not always allow the object to enter only when the age of the object reaches a certain level. In the old generation, if the total memory of objects of the same age in the survivor space is greater than half of the memory in the survivor space, objects greater than or equal to this age will enter the old generation.

  • Space allocation guarantee
    The old generation will guarantee the memory allocation of the new generation. That is to say, before performing Minor GC, the virtual machine will first check the maximum continuous available memory of the old generation. Is the space greater than or equal to the total space of all objects in the new generation? If it is greater than or equal to this, it means that the Minor GC is safe this time, because after the Minor GC, the new generation objects may be stored in the old generation (when the survivor space is lost after the Minor GC When the memory is not enough), if this condition is not true, the virtual machine will check whether the HandlePromotionFailure setting allows guarantee failure. If it is allowed, the virtual machine will check whether the maximum continuous available space in the old generation is greater than the average size of objects that have been promoted to the old generation. If it is greater, The virtual machine attempts to perform a Minor GC. If guarantee failure is not allowed, Full GC (Old Generation GC) will be performed to reclaim the space of dead objects in the Old Generation so that the Old Generation can free up more space.

The above is the detailed content of How to allocate and manage JVM memory in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
jvm
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!