Memory management for Java virtual machines

Source: Internet
Author: User

It is well known that Java programmers write code that has no way to control the memory of Java objects freed, completely with the JVM black-out operation.

Although the programmer has given the task of releasing the memory to the Java Virtual machine, it does not mean that there is no memory leak in the Java program.

On the contrary, in a way, when a memory leak occurs, debug becomes much more difficult.

Therefore, Java programmers, it is necessary to understand the Java Virtual machine for memory management and garbage collection mechanism.

How does a Java virtual machine determine that an object can be recycled?

When an object is not referenced by any other, the object is considered recyclable by the Java virtual machine.

An early-risers virtual machine that uses a reference count method to determine whether an object is recyclable. The benefits of this approach are simple and efficient, but it is difficult to solve the problem of circular references.

Now the mainstream Java Virtual machine is the one that uses accessibility to analyze whether objects are recyclable. From a number of root nodes, all objects that can be reached from the root node are surviving objects.

Conversely, other objects are recyclable objects. As you can imagine, this is a graph with a number of root nodes, and the term of graph theory is to judge the accessibility of each node.

What are strong references, soft references, weak references, and virtual references?

Strong reference strong reference is usually the most common, without any modification of the object instantiation, get a strong reference.

Soft references soft Reference, soft-referenced objects, when the Java Virtual machine garbage collection occurs when there is insufficient space, the soft-referenced objects will be recycled.

Weak references weak Reference, weakly referenced objects, are recycled in any case when the Java virtual machine is next garbage collected.

Virtual Reference Phantom Reference, a virtual Reference object cannot get an instance of its object at any one time. The use of virtual references is to keep track of the life and death of an object, and when a virtual reference is garbage collected, a system notification is received.

What are the garbage collection algorithms?

1. Mark Clear, as the name implies is the mark and clear two stages. The fatal weakness of this algorithm is that it generates a large amount of memory fragmentation, resulting in a later failure to allocate large contiguous memory space even if there is enough memory space.

2. The replication algorithm is to divide the memory space into two regions of equal size, using only one of the regions at a time. Each time the memory is reclaimed, the surviving object is copied to another area.

Although this algorithm solves the problem of memory fragmentation, it is at the expense of shrinking the memory by half.

Replication algorithms are also commonly used in the new generation of garbage collection. Because most of the objects are going to die, they will not survive the next round of garbage collection. So it's not really necessary to divide the memory space by 1:1.

In reality, memory is divided into a larger Eden area and two smaller survivor areas. The eden:survivor:survivor=8:1:1 of the hotspot virtual machine.

Each time you use Eden and one of the survivor to allocate memory, and when garbage is collected, the surviving objects are copied to another survivor area.

When there is insufficient memory in the survivor, the memory space of the old age is used. The old generation is a guarantee of the role.

3. Mark sorting algorithm. Mark the surviving objects and move them to one end of the memory space, freeing up the memory space outside the end boundary. The old age generally uses the mark finishing algorithm.

4. Generational collection algorithms. The memory space is generally divided into the old and the new generation, different regions according to the survival cycle and characteristics of the object, the use of appropriate garbage collection algorithm.

The new generation uses the replication algorithm. The old age uses the mark sweep or the labeling collation algorithm.

Memory management for Java virtual machines

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.