- Mastering High Performance with Kotlin
- Igor Kucherenko
- 501字
- 2021-06-25 20:55:21
Working principles of the garbage collector
The garbage collection strategy assumes that the developer doesn't explicitly release memory. Instead, the garbage collector (GC) finds objects that aren't being used anymore and destroys them. As GC sees it, there are two types of objects—reachable and unreachable. This principle is based on a set of root objects that are always reachable. An object is a root if it satisfies one of the following criteria:
- Local variables: They are stored in the stack of a thread. When a method, constructor, or initialization block is entered, local variables are created and become unreachable and available for the GC once they exit the scope of the method, constructor, or initialization block.
- Active threads: These are objects that hold other ones from the GC's point of view. So all these objects are a reference tree that will not be destroyed until the thread is terminated.
- Static variables: They are referenced by instances of the Class type where they're defined. The metadata of classes is kept in the Metaspace section of memory. This makes static variables de facto roots. When a classLoader loads and instantiates a new object of the Class type, static variables are created and can be destroyed during major garbage collection.
- Java native interface references: They are references to objects that are held in native code. These objects aren't available to the GC because they can be used outside the JVM environment. These references require manual management in native code. That's why they often become the reason for memory leaks and performance issues.
The following diagram illustrates a simplified schematic of references trees:

An object is reachable if it's a leaf from a reference tree that's reachable from the root object. If an object is unreachable, then it's available for the GC. Since the GC starts collecting at unpredictable times, it's hard to tell when the memory space will be deallocated.
To perform garbage collection, the JVM needs to stop the world. This means that the JVM stops all threads except those that are needed for garbage collection. This procedure guarantees that new objects aren't created and that old objects don't suddenly become unreachable while the GC is working. Modern GC implementations do as much work as possible in the background thread. For instance, the mark and sweep algorithm marks reachable objects while the application continues to run, and parallel collectors split a space of the heap into small sections, and a separate thread works on each one.
The memory space is pided into several primary generations—young, old, and permanent. The permanent generation contains static members and the metadata about classes and methods. Newly created objects belong to the young generation, and once there's no reference to any of them, it becomes available for minor garbage collection. After this, the surviving objects are moved to the old generation, and become available only for major garbage collection.
- 劍指Offer(專項突破版):數據結構與算法名企面試題精講
- JavaScript語言精髓與編程實踐(第3版)
- Learning Linux Binary Analysis
- Windows Server 2012 Unified Remote Access Planning and Deployment
- PHP+MySQL網站開發項目式教程
- 飛槳PaddlePaddle深度學習實戰
- Python算法指南:程序員經典算法分析與實現
- HTML 5與CSS 3權威指南(第3版·上冊)
- UML2面向對象分析與設計(第2版)
- 零基礎學Scratch 3.0編程
- Sails.js Essentials
- UX Design for Mobile
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- 優化驅動的設計方法
- R語言數據分析從入門到實戰