1. In Java, when can objects be garbage collected?
When an object to the GC roots unreachable, in the next garbage collection cycle will attempt to reclaim the object, if the object overrides (five) Finalize () method, and the method re-assigned to a reference to achieve self-help effect, then this object will not be recycled, if not " Save, the object will be recycled.
How does the garbage collection mechanism work in 2.Java?
When an object is instantiated (because the garbage collection mechanism is in the heap), the first thing to do is to see if there is enough space in the Eden area, and if it is sufficient, the object is stored directly, not garbage collected, and if Eden Memory is full of minor GC (the function is to reclaim the garbage to free memory), To see if Eden memory is enough, if it's not enough, you need to see if the survivor area is enough memory (trying to save the Eden part of the active object in survivor), if enough, Save the Eden part of the active object in survivor, which is stored in Eden, if not enough, to see if the old age is enough memory, if enough, to save the survivor part of the active object in the old age, the Eden part active object is saved in Survivor, This object is stored in Eden, and if it is not enough, a full GC will be performed, and then the old generation will then be able to determine if the memory is sufficient, if not enough, if not enough (causing the JVM to not create a memory region for the new object in the Eden Zone), the OutOfMemoryError will be thrown.
Java interview frequently test knowledge points