0% found this document useful (0 votes)
38 views11 pages

Garbage Collector

1. Garbage collection is the process of reclaiming unused memory automatically in Java. 2. The garbage collector removes unreferenced objects from heap memory, making Java memory efficient without requiring manual memory management. 3. Some techniques for making objects eligible for garbage collection include nullifying references, reassigning references, and using island isolation references.

Uploaded by

Krishna Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views11 pages

Garbage Collector

1. Garbage collection is the process of reclaiming unused memory automatically in Java. 2. The garbage collector removes unreferenced objects from heap memory, making Java memory efficient without requiring manual memory management. 3. Some techniques for making objects eligible for garbage collection include nullifying references, reassigning references, and using island isolation references.

Uploaded by

Krishna Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Garbage Collector

Garbage means unreferenced or


unusable objects.

Garbage collection is process of


reclaiming the runtime unused memory
automatically.

When it comes to C/C++ if we want to


clean or clear the space the programmer
needs to do it manually.

Java provides better memory


management than C/C++

It makes java memory efficient because


garbage collector removes the
unreferenced objects from heap
memory.
It is automatically done by the garbage
collector (a part of JVM).

So we don’t need to make an extra


efforts to make the free space.

1. By nullifying reference.
2. Reassigning reference.
3. Iland Isolation Reference.

Nullifying/ Nulling Reference:

Student S = new students ()


S = null --- It will create the
access for garbage collector. It’s
nothing but nullifying reference.
Reassigning Reference:

Employee e1 = new Employee();


Employee e2 = new Employee();
e1 = e2;
//now the first object referred by e1 is
available for garbage collection.

Iland Isolation Reference:

Student s1 = new Student ();


Student s2 = new student ();
S1 = null;
S2 = null;

Both will eligible for the GC


Finalization:

Whenever the GC collect the object


before finalize method Is called to clean
up the activities of that particular
object.
Ex:

Class Test ()
{
Public static void main(String arg[])
String s1 = new string();
s1 = null;
System.gc();
}
}
public void finalize() throws
throwable{
System.out.println(“Finalize”);
}
}
gc()

The gc() method is used to invoke the


garbage collector to perform cleanup
processing. The gc() is found in system
and Runtime classes

Public static void gc(){

Example:-
Simple Example of Garbage Collection
in Java.

public class TestGarbage1{


public void finalize (){
System.out.println(“object is garbage
collected”);
}
public static void main(String args[])
{
TestGarbage1 s1 = new
TestGarbage1();
TestGarbage1 s2 = new TestGarbage1
();
s1 = null;
s2 = null;
System.gc();
}
}
O/p:
object is garbage collected
object is garbage collected.

1. Why garbage collection is


necessary in Java.?
2. What is the drawback to garbage
collection?
The main drawback is it freezes all
those threads which are currently
active at the time of memory
recovery phase.
3. Use of the Finalize() Method?
4. Can we force the garbage collector
to run at anytime?

Loggers – (log4j)
Log 4j is the framework

[email protected]

Facebook.com (web
application)
Amazon.com

Flipkart.com.

Log4j is the framework which


is provided by apache
framework.
The responsibility of the log4j
is to traceout the issue in
application level.

We can use java.utili.loggers to


log the messages

Apache 4j is famous for


logging the message.
Loggers Level/Log4j levels:

1. Trace level – To trace out


the issue.
2. Debug level – To rectify
the issue or bug.
3. Info level – To provide
complete information in
the info level.
4. Warn level – It will
provide or give warning to
the application that facing
an issue.
5. Error Level- Trace out the
issue in the error level.
6. Fatal Level – The log level
that tells the application
encounter an event or
entered a state in which
one of the crucial business
functionality is no
working.

You might also like