A brief analysis of the heap and 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;
- Insufficient space in the method area;
- Call System.gc(), it is recommended that the JVM perform full gc;
- Long-term surviving objects are transferred to the old generation , insufficient space;
- There is not enough contiguous space allocated to large objects;
- 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.
##Reference counting method
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.
#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.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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 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

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.

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.

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

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
