Home Web Front-end JS Tutorial A brief analysis of the heap and garbage collection mechanism

A brief analysis of the heap and garbage collection mechanism

Jul 03, 2020 am 09:20 AM
Garbage collection mechanism

In this article we mainly focus on these issues:: After the Java program is executed, when will the objects in the heap be recycled? How to recycle?

The heap is also called the "GC heap." Since collectors now basically use generational collection algorithms, the Java heap can also be subdivided into: new generation and old generation. The ratio is 1:2; to be more detailed, the new generation is divided into Eden area and Survivor area, and the ratio is 8:1. The following figure shows the structure of the heap:

##The allocation of memory for objects in the heap is strictly regulated , the strategy is:

  • Objects allocate memory in the new generation Eden area first;

  • ## Large objects enter the old generation directly, mainly long strings and arrays, which require a large amount of continuous memory space;

  • Long-term surviving objects enter the old generation. When the memory in the Eden area is insufficient, the JVM initiates a MinorGC, and the age of the object is increased by one. The default object age reaches 15 and enters the old age;

  • Dynamic age determination. The sum of the sizes of all objects of the same age is greater than half of the Survivor space. Objects greater than or equal to this age enter the old generation

The new generation GC refers to the Minor GC. Garbage collection in the new generation is frequent and fast. Old generation GC (Major GC/Full GC) performs garbage collection in the old generation, usually accompanied by at least one minor gc. Slow. Full GC will be triggered in the following situations:

    Insufficient space in the old generation;
  1. Insufficient space in the method area;
  2. Call System.gc(), it is recommended that the JVM perform full gc;

  3. Long-term surviving objects are transferred to the old generation , insufficient space;

  4. There is not enough contiguous space allocated to large objects;

  5. There are too many objects that survive garbage collection in the new generation, and S1 cannot fit them in. The guaranteed space in the old generation is insufficient. The guaranteed space refers to whether the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation.

Almost all objects are placed in the heap, so how do we know whether these objects are still useful? JVM provides two methods to determine:

  • ##Reference counting method: The object adds a reference counter. Every time it is referenced, the counter value increases by one. When the reference becomes invalid, the counter value decreases by one. When the number of references is 0, it means that the object is not alive. The reference counting method cannot solve the circular reference problem. There are detailed examples in Teacher Zhou Zhipeng's book, which is relatively easy to understand.

  • #reachability analysis method : Taking the "GC Roots" object as the starting point, just like the root node of a tree, search downwards. The path traveled by the search is called a reference chain. If there is no reference chain from an object to the starting point of GC Roots, then this The object is unreachable and needs to be recycled. GC Roots refers to objects referenced by the virtual machine stack, objects referenced by the local method stack, objects referenced by static properties in the method area, and objects referenced by constants in the method area.

## References were mentioned above. The survival of objects is related to references. Reference types are divided into strong references, soft references, weak references, and virtual references.

Strong reference, new object, the garbage collector will never recycle it;

  • Soft reference, the memory of these objects will be recycled before OMM occurs in the system;

  • Weak reference, as soon as the garbage collector finds it when it is working, it will be recycled immediately ;

  • Virtual reference is useless and may be recycled at any time.

In fact, unreachable objects determined by the reachability analysis method will not be recycled immediately. The object needs to be marked twice before it is actually recycled. . The first marking is to determine that the object is unreachable, and then a filter is performed. The filter condition is whether it is necessary to execute the finalize() method of this object. If the finalize() method is not overridden or the finalize() method has been called by the virtual machine, the finalize() method will only be called once by the system. In both cases, there is "no need to execute". If necessary, this object will be placed in the F-Quene queue, a low-priority automatically created by the virtual machine. FinalizerThe thread executes the finalize() method. During this period, the GC will mark the object in F-Quene for a second small scale. If the object is still not referenced, it will be recycled. Objects that are not filtered are not necessarily recycled.

#We already know what the object is Time has been recycled, so how to recycle it? Introducing the four most commonly used garbage collection algorithms:

  • Mark-clear: mark the objects that need to be cleared first, and then collect them uniformly ---- not efficient, will Generates a large number of discontinuous fragments;

  • Copy algorithm: Divide the memory into blocks, use only one block at a time, and copy the surviving objects to another after use. On one piece;

  • Marking and sorting: mark the surviving objects first, then move all surviving objects to one end, and directly clean up the memory outside the end boundary;

  • Generational algorithm, the heap is divided into the new generation and the old generation. A large number of objects will die every time the new generation is collected, so choose the copy algorithm. The survival rate of the old generation is relatively high, and there is no extra space for allocation guarantee, so choose the mark clearing or mark sorting algorithm.

The garbage collection algorithm is an idea of ​​memory recycling, and the specific implementation is a garbage collector. A brief introduction to commonly used garbage collectors:

  • serial serial collector. Single thread, other work must be suspended during garbage collection. Copying for new students, labeling for old people. Simple and efficient;

  • ParNew collector. The multi-threaded version of serial;

  • Parallel Scavenge collector, a multi-threaded collector of the replication algorithm. Pay attention to throughput, cpu running code time / total cpu time spent. New generation copy, old mark sorting;

  • Serial Old collector, old generation version;

  • Parallel Old collector, Parallel Scavenge old generation version;

  • CMS collector, focusing on the shortest pause. With a concurrent collector, the garbage collection thread works (basically) simultaneously with the user thread. Mark-and-sweep algorithm

For more details about the garbage collector, you can read Mr. Zhou Zhipeng’s book.

Recommended tutorial: "

JS Tutorial"

The above is the detailed content of A brief analysis of the heap and garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article to talk about the garbage collection mechanism in php An article to talk about the garbage collection mechanism in php Aug 26, 2022 am 10:48 AM

This article will give you an in-depth understanding of the garbage collection mechanism in PHP. I hope it will be helpful to you!

Decrypting the memory management and garbage collection mechanism of Go language Decrypting the memory management and garbage collection mechanism of Go language Nov 30, 2023 am 09:17 AM

Go language is an efficient, safe, and concurrent programming language. The design of memory management and garbage collection mechanism is also its unique feature. This article will decrypt the memory management and garbage collection mechanism of Go language in depth. 1. Memory management In the Go language, memory management includes two aspects: memory allocation and memory release. 1.1 Memory allocation In the Go language, we allocate memory through the built-in functions new and make. Among them, new returns a pointer to the newly allocated zero value, while make returns a specified type and its length.

How to solve common problems of memory release in Java functions? How to solve common problems of memory release in Java functions? May 02, 2024 am 09:57 AM

Memory management in Java involves garbage collection, but problems can still arise. Common problems include memory leaks and memory fragmentation. Memory leaks are caused by objects holding references that are no longer needed and can be solved by avoiding reference cycles, using weak references, and limiting variable scope. Memory fragmentation is caused by frequent allocation and deallocation and can be solved by using memory pools, large object pools, and compact garbage collection. For example, using weak references can handle memory leaks and ensure that the garbage collector reclaims objects when they are no longer needed.

In-depth understanding of the underlying development principles of PHP: memory management and garbage collection mechanism In-depth understanding of the underlying development principles of PHP: memory management and garbage collection mechanism Sep 10, 2023 pm 02:30 PM

In-depth understanding of the underlying development principles of PHP: memory management and garbage collection mechanism Introduction: PHP, as a high-level programming language, is widely used in Web development. Many developers are familiar with PHP's syntax and features, but may have relatively little understanding of PHP's underlying development principles. This article will deeply explore the memory management and garbage collection mechanisms in the underlying development principles of PHP to help readers better understand the operating mechanism of PHP. 1. PHP’s memory management Memory allocation and release Memory management in PHP is handled by the Zend engine

An in-depth analysis of the garbage collection mechanism in Python An in-depth analysis of the garbage collection mechanism in Python Mar 29, 2018 pm 01:20 PM

Thanks to Python's automatic garbage collection mechanism, there is no need to manually release objects when creating them in Python. This is very developer friendly and frees developers from having to worry about low-level memory management. But if you don’t understand its garbage collection mechanism, the Python code you write will often be very inefficient.

An in-depth analysis of the garbage collection mechanism in JS An in-depth analysis of the garbage collection mechanism in JS Mar 02, 2023 pm 07:31 PM

Primitive types are stored on the stack, and reference types are stored on the heap. JavaScript automatically allocates memory when variables (objects, strings, etc.) are created, and "automatically" releases them when they are not used. The process of releasing is called garbage collection.

Explore the memory management features and garbage collection mechanism of Go language Explore the memory management features and garbage collection mechanism of Go language Jan 23, 2024 am 10:07 AM

Exploring the garbage collection mechanism and memory management features of Go language Introduction: With the development of the Internet, developers have increasingly higher requirements for programming languages. As a statically typed, compiled language, Go language has attracted much attention since its inception due to its efficient garbage collection mechanism and memory management features. This article aims to deeply explore the garbage collection mechanism of the Go language and its memory management features, and help readers better understand and utilize these features through specific code examples. 1. Garbage collection mechanism 1.1 mark-scan algorithm of Go language

Explore: Different development stages of JVM garbage collection mechanism Explore: Different development stages of JVM garbage collection mechanism Feb 23, 2024 pm 05:36 PM

In-depth analysis: The diversified evolution of the JVM garbage collection mechanism requires specific code examples 1. Introduction With the development of computer science, the garbage collection mechanism plays a vital role in the JVM (Java Virtual Machine). The diversified evolution of the JVM garbage collection mechanism is to improve the performance and memory management of Java programs. This article will provide an in-depth analysis of the specific evolution of the JVM garbage collection mechanism and provide specific code examples to help readers better understand. 2. The basic principles of garbage collection mechanism in explaining the JVM garbage collection mechanism

See all articles