Block of memory at JVM runtime
- PC Register: (thread exclusive) Save thread run Java method address
- Java Virtual stack (thread exclusive): stack frame for storing threads
- Java heap (global share): As the startup of a Java Virtual machine creates an instance of all objects and an array object, and a built-in GC (garbage collection Mechanism)
- Method Area: Run a constant pool
- Local method Stack: thread-exclusive.
The GC (garbage collection mechanism) in the JVM is one of the Java standpoints. Automatic garbage collection allows Java programmers to reduce their ability to deal with memory and focus on business development and implementation.
The following points are required for garbage collection
- What objects can be recycled '
- When to recycle
- How to Recycle
There are two main types of object definitions for garbage collection:
- Check if this object has a reference
For example: a =new a (); Then object A is quoted. But this way there is a deadlock, that is, there are two or more objects referencing each other, but these objects are useless. What to do?
- Check to see if the object is up to the root object.
Only one root,root is directly or indirectly associated with other objects, and if the object is not reached at the root, then the object is "garbage"
There is no deadlock in the way that the root can be reached, which is the mainstream implementation of the JVM GC at present. Of course, the future is still a long way, perhaps there is a better algorithm to achieve it?
When is it recycled?
The generation of garbage objects is usually cycle recovery. A Java program that stops running when garbage is recycled
How to recycle?
- Mark-Clean: Mark the object as garbage first, then clean it up so that the space is not contiguous
- Copy: Divides the memory into two halves and then copies the valid copy to the other half. This sort of memory space is contiguous.
- Mark-Clean-up: Optimize for tag-cleanup and organize contiguous memory space.
Further optimization:
There are always some objects that will always exist such as static variables, thread pools ... These "old undead" there is no need to always check them, can reduce the inspection and cleaning cycle. So there is, the new generation, Laosheng generation, permanent generation
- Cenozoic : New objects, which are usually local objects, are soon extinct after use. After several cycles, it will be upgraded to Laosheng generation with a survival rate of around 10%
- Laosheng Generation : From the new generation upgraded to the Laosheng generation, has a more tenacious life, so the garbage cleanup algorithm and cycle are different from the Cenozoic
- permanent Generation : the permanent generation is not impossible to die, clean or want is only a long period.
Elementary knowledge of Jvm--java virtual machine