Java has a very good memory management system. When an object in Java is no longer useful, or we can say that we will not require that object in the future, it is destroyed. The memory space is now available for any other purpose that was previously occupied. The Garbage Collector in Java handles the entire process.
Garbage collection (or GC) is a method of reclaiming memory that is no longer in use for reuse. Unlike other languages that require manual allocation and destruction of objects, Java programmers do not need to pick up and study each object to determine whether it is required.
The garbage collector is the perfect example of a Daemon thread because it runs in the background all the time.
DAEMON THREAD
A daemon thread is a low-priority thread that performs operations such as garbage collection in the background.
Properties:
- The daemon status of a newly generated thread is inherited by it. Because the main thread is non-daemon, all threads formed within it (children of the main thread) are non-daemon by default. Using the setDaemon() method of the thread class, you can convert a user thread to a Daemon.
- public void setDaemon(boolean status): This method is used to determine whether or not current is a daemon. If the thread is Daemon, it returns true; otherwise, it returns false.
Daemon VS User Thread
- Thread Priority: Because user threads have a higher priority than daemon threads, they won't obtain as much CPU as a daemon thread.
- Creation of Thread: The application normally creates a user thread to do multiple tasks at the same time. Daemon threads, on the other hand, are typically created by JVM for garbage collection purposes.
- Termination of Thread: If all user threads have completed their execution but the user thread is terminated by the program or by itself, the JVM will cause the daemon thread to end. A user thread can continue to execute while the JVM is executing, but a daemon thread cannot. This is the most important distinction between user and daemon threads.
In Java, there are two sorts of garbage collection activities that typically occur:
- When inaccessible objects in the young generation heap memory are deleted, a minor or incremental garbage collection occurs.
- When the items that survived the minor garbage collection and were copied into the old generation or permanent generation heap memory are eliminated, it is said that a large or full garbage collection has occurred.
How to make an object GC-eligible:
- Even while the programmer is not responsible for destroying unnecessary things, it is highly suggested that if an object is no longer required, it be made inaccessible (thus eligible for GC).
- In general, there are four possible approaches to make an object garbage-collectible.
- Nullifying the reference variable
- Re-assigning the reference variable
- Object created inside method
- Islands of Isolation
Island of Isolation(IOI)
- Reduce the number of objects sent to the old generation area as much as possible.
- Set the major (or entire) GC time to be as short as possible.
Comments
Post a Comment