Garbage Collection
- Garbage Collection is process of reclaiming the runtime unused memory automatically
- It is a way to destroy the unused objects
- Java is performed automatically, Java provides better memory management
1. Advantage
- It makes java memory efficient
- It is automatically done by the garbage collector(a part of JVM)
2. How can an object be unreferenced?
- By nulling the reference
- By assigning a reference to another
- By annoymous object etc
1) By nulling a reference
2) By assigning a reference to another
3) By annonymous object
3. finalize() method
- The finalize() is invoked each time before the object is garbage collected
- It can be used to perform cleanup processing
1) point
- The garbage collector collects only those objects that are created by new keyword
- If you have created any object without new, you can use finalize method to perform cleanup processing
4. gc() method
- gc() is used to invoke the garbage collector to perform cleanup processing
- gc() is found in System and Runtime classes
1) point
- Garbage collection is performed by a daemon thread called GC
- This thread calls the finalize() before object is garbage collected