A thread-private area of memory is reclaimed as the user thread ends, the memory allocation compilation period is determined, and memory allocation and reclamation are deterministic. Shared threads are created and destroyed as virtual machines start, end, and are dynamically allocated at run time. The garbage collector primarily collects garbage collection for shared memory areas (heaps and method areas).
How does Java implement memory dynamic allocation and memory garbage collection?
1. What memory needs to be recycled (garbage collector memory reclaims objects)? Has been "dead" object, how to judge the object has been "dead"?
Memory reclaimed by the Java heap: objects that have been "dead"
Memory reclaimed by method area: Obsolete constants and useless classes
2. When to recycle? Under what circumstances does the garbage collector automatically reclaim memory?
3, how to recycle? How does a Java Virtual machine implement memory reclamation?
(i) How to judge the object in the Java heap as "dead"
1. Reference Counting algorithm:
Algorithm idea: Through the reference counter of the object to judge, the reference time increment counter, the reference failure after the decrement counter.
Cons: Difficult to solve circular reference problems
2, accessibility analysis algorithm:
Algorithm idea: To determine whether the object to the GC root node has a reference chain, if it does not exist, it means that the object is not available.
Cons: (1) when the GC root point is more time to check the time consuming too much
(2) When performing the analysis, the GC needs to pause and pause all Java execution threads.
GC Root: There is a major global reference (constant, static property), execution context (such as the local variable table of the stack frame),
Specific to the GC root node are:
1) objects referenced in the virtual machine stack (local variable table within the stack frame)
2) static properties in the method area and objects referenced by constants
3) objects referenced by local methods in the local method stack
3. Classification of object references
Reference type |
Features and Uses |
Memory Recycling Time |
Realize |
Strong references |
Manipulate the Reference object. |
Non-recyclable |
A reference similar to Object Boj=new object (); |
Soft references |
Used to describe useful but not necessary objects, which are weaker than strong references |
Second recycling |
SoftReference |
Weak references |
Used to describe useful but not necessary objects, which are weaker than soft references |
The next garbage collection, whether or not the memory is sufficient, is recycled. |
WeakReference |
Virtual use |
The implementation object receives a system notification when the garbage collector recycles. The weakest reference relationship |
The next time the garbage collection |
Phantomreference |
(b) How to judge discarded constants and useless classes in the method area?
1. How to determine the discarded constants
Similar to the survival judgment of the heap memory object
2, how to determine the useless class
Conditions to become useless classes:
(1) All instance objects have been recycled
(2) The ClassLoader that loaded the class have been recycled
(3) class information does not exist for reference and reflection access
(c) How does the garbage collector reclaim memory?
1, marking-clearing algorithm 2, copy algorithm 3, marker-collation algorithm 4, generational collection algorithm (1) algorithm implementation: (2) heap memory partition:
|
Cenozoic |
Old age |
Survival |
Low survival rate |
High survival rate |
Recovery algorithm |
Replication algorithm (Eden,from survivor,to Survivor) |
Tag-cleanup algorithm; marker-Collation algorithm |
Is there any additional space guarantee |
Yes |
No |
(d) How to allocate memory? (Allocation policy for memory)
1. Object priority in Eden allocation
2, large objects (objects that require a large amount of contiguous memory space) go straight to the old age
3, long-term survival of the object directly into the old age
Second, how does Java allocate and reclaim memory? How does the Java garbage collector work?