- 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.
- Java語言程序設計
- 深入理解Android(卷I)
- Boost C++ Application Development Cookbook(Second Edition)
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Rake Task Management Essentials
- R的極客理想:工具篇
- Learning Selenium Testing Tools(Third Edition)
- Teaching with Google Classroom
- 圖數據庫實戰
- 創意UI:Photoshop玩轉APP設計
- Vue.js光速入門及企業項目開發實戰
- Learning Concurrency in Python
- 微前端設計與實現
- 計算機系統解密:從理解計算機到編寫高效代碼
- Developing Java Applications with Spring and Spring Boot