Java programs automatically manage their memory through a process known
as garbage collection. Java programs can be executed on a Java Virtual
Machine, or JVM, by compiling to bytecode. Objects are created on the
heap, a section of memory reserved for the program, when Java programs
are run on the JVM. Some things will eventually become obsolete. To free
up memory, the garbage collector discovers these unused objects and
deletes them.
A programmer is in charge of both creating and destroying objects in
C/C++. Programmers frequently forget to destroy useless objects.
This carelessness may eventually prevent enough memory from being
available to create new objects, resulting in an abnormal program
termination and OutOfMemoryErrors.
The programmer doesn't need to be concerned about unused objects in Java as they
are automatically handled by the garbage collector. The job of the garbage
collector is to make free memory space by destroying objects that are no longer
accessible. The garbage collector can be likened to the Daemon thread as it
continually operates in the background.
Java employs an automatic garbage collection system, which involves scanning the
heap memory to identify objects that are actively used and those that are not.
The unused objects are then deleted. An object is considered in use or
referenced if a part of the program still holds a pointer to it.
The memory that an object has taken up can be freed up when it is no longer
referenced or used by any part of the programme.Programmers do not need to
explicitly mark objects for deletion because the Java Virtual Machine (JVM)
automatically takes care of garbage collection.
How does an object become unreferenced?
1.By nulling a reference
Student stu = new Student();
stu=null;
2.By assigning a reference to another
Student stu1 = new Student();
Student stu2 = new Student();
stu1=stu2;
Garbage collection (GC) in Java is handled by a daemon thread known as the
Garbage Collector. The finalise() method is used by this thread just before an
object is garbage collected.
The JVM's garbage collector only remove objects that were created with the "new"
keyword. If any objects were created without using "new," the finalize() method
can be utilized to perform cleanup operations and destroy those remaining
objects.
Each time an object becomes eligible for garbage collection, the finalize()
method is called, providing an opportunity for cleanup processing. This method
is defined in the Object class.
To initiate the garbage collection process, the gc() method is invoked. This
method is present in the System and Runtime classes and is responsible for
triggering cleanup operations.
Types of Java Garbage Collection Activities
In Java, two types of garbage collection activity are common as given below
Garbage collection that is minor or incremental : It is thought to have
happened when unreachable objects in young generation heap memory were removed.
Garbage Collection, Major or Complete : It is said to have happened when
the objects that survived the minor garbage collection were copied into the old
generation or permanent generation heap memory and then removed. Garbage
collection occurs less frequently in the elderly than in the younger generation.
Post your comment