What is garbage collection in Java, and how can it be used ?

Answers were Sorted based on User's Feedback



What is garbage collection in Java, and how can it be used ?..

Answer / faiz misbah iiit-bangalore

Each time an object is created in java,it goes into an area
of memory known as the HEAP.All objects-no matter when.
where or how they're created -live on the heap.But it's not
just old memory heap;the java heap is actually called the
Garbage-collectible Heap.When you create an object, java
allocates memory space on the heap according to how much
that particular object needs.java manage that memory for
you!When the JVM can 'see' that an object can never be used
again,that object become eligible for garbage collection.and
if you're running low on memory,the garbage collector will
run,throw out the unreachable objects,and free up the
space.so that the space can be reused.

Run object finalization using System class

public class Main {

public static void main(String[] args) {
System.runFinalization();
}
}
Run the garbage collector using System class

public class Main {

public static void main(String[] args) {

System.gc();

}

}

The Lifetime of an Object

class Sphere {
double radius; // Radius of a sphere

Sphere() {

}

// Class constructor
Sphere(double theRadius) {
radius = theRadius; // Set the radius

}
}

public class MainClass {
public static void main(String[] arg){
Sphere sp = new Sphere();

System.gc();
}

}

Is This Answer Correct ?    13 Yes 5 No

What is garbage collection in Java, and how can it be used ?..

Answer / ashish

Garbage collection is the Process of automatically freeing
objects that are no longer referenced by the Program.
But a Potential disadvantages of a garbage-collected heap is
that it adds an overhead that can affect program performance....

Is This Answer Correct ?    2 Yes 0 No

What is garbage collection in Java, and how can it be used ?..

Answer / praveen thakur suryavanshi

The JVM's heap stores all objects created by an executing
Java program. Objects are created by Java's "new" operator,
and memory for new objects is allocated on the heap at run
time. Garbage collection is the process of automatically
freeing objects that are no longer referenced by the
program. This frees the programmer from having to keep track
of when to free allocated memory, thereby preventing many
potential bugs and headaches.

The name "garbage collection" implies that objects that are
no longer needed by the program are "garbage" and can be
thrown away. A more accurate and up-to-date metaphor might
be "memory recycling." When an object is no longer
referenced by the program, the heap space it occupies must
be recycled so that the space is available for subsequent
new objects. The garbage collector must somehow determine
which objects are no longer referenced by the program and
make available the heap space occupied by such unreferenced
objects. In the process of freeing unreferenced objects, the
garbage collector must run any finalizers of objects being
freed.

Is This Answer Correct ?    4 Yes 3 No

What is garbage collection in Java, and how can it be used ?..

Answer / prashant srivastava the great

The JVM's heap stores all objects created by an executing
Java program. Objects are created by Java's "new" operator,
and memory for new objects is allocated on the heap at run
time. Garbage collection is the process of automatically
freeing objects that are no longer referenced by the
program. This frees the programmer from having to keep track
of when to free allocated memory, thereby preventing many
potential bugs and headaches.

The name "garbage collection" implies that objects that are
no longer needed by the program are "garbage" and can be
thrown away. A more accurate and up-to-date metaphor might
be "memory recycling." When an object is no longer
referenced by the program, the heap space it occupies must
be recycled so that the space is available for subsequent
new objects. The garbage collector must somehow determine
which objects are no longer referenced by the program and
make available the heap space occupied by such unreferenced
objects. In the process of freeing unreferenced objects, the
garbage collector must run any finalizers of objects being
freed.

Is This Answer Correct ?    2 Yes 1 No

What is garbage collection in Java, and how can it be used ?..

Answer / siva

Garbage collection is a thread, that runs to reclaim the
memory by destroying objects which object is cannot be
referenced anymore.
instead of calling destructor JVM do it automatically, as we
use destructor in c++ to destroy the object

Is This Answer Correct ?    1 Yes 0 No

What is garbage collection in Java, and how can it be used ?..

Answer / jitendra

An application running on a system computer uses some
memory, which makes memory management a significant issue
for any programming language. As Java is comparatively high-
level language, the memory management in Java is automatic.
To make it more efficient, we need to understand garbage
collection, i.e. freeing memory from objects that are no
longer in use.
Garbage collection is the process of automatically freeing
objects that are no longer referenced by the program. This
frees a programmer from having to keep track of when to
free allocated memory, thus preventing many potential bugs
and problems.

Is This Answer Correct ?    1 Yes 0 No

What is garbage collection in Java, and how can it be used ?..

Answer / jaffer

Actually garbage collector is a daemon thread. Normally
JVM’s heap stores all objects which is created by new
operator. So objects are occupying memory in heap. Some
objects are not used for long time. So garbage collector do
that work automatically.
Otherwise we call finalization method

Is This Answer Correct ?    1 Yes 0 No

What is garbage collection in Java, and how can it be used ?..

Answer / brijesh yadav

Garbage Collection is a form of automatic memory
management. The garbage collector or collector attempts to
reclaim the memory used by objects that will never be
accessed again by the application or mutator.

Is This Answer Correct ?    0 Yes 0 No

What is garbage collection in Java, and how can it be used ?..

Answer / rintu kumar kanp

Finding garbage and reclaiming memory allocated to it.
Garbage holds unreferenced objects
ex:-
Student ali= new Student();
Student khalid= new Student();
ali=khalid;
Now ali Object becomes a garbage,
It is unreferenced Object


it be used When the total memory allocated to a Java
program exceeds some threshold.

Is This Answer Correct ?    0 Yes 0 No

What is garbage collection in Java, and how can it be used ?..

Answer / poonam

In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Garbage collection was invented by John McCarthy around 1959 to solve problems in Lisp.[1][2]
Garbage collection is often portrayed as the opposite of manual memory management, which requires the programmer to specify which objects to deallocate and return to the memory system. However, many systems use a combination of the two approaches, and other techniques such as stack allocation and region inference can carve off parts of the problem. There is an ambiguity of terms, as theory often uses the terms manual garbage collection and automatic garbage collection rather than manual memory management and garbage collection, and does not restrict garbage collection to memory management, rather considering that any logical or physical resource may be garbage collected.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

what is inner class?

6 Answers   HCL,


How are the elements of a gridbaglayout organized?

0 Answers  


What is the difference between Array and Hash Table?

0 Answers   Impetus,


Can an interface have a class?

0 Answers  


Difference between static synchronization vs. Instance synchronization?

0 Answers  






How can a gui component handle its own events?

0 Answers  


What is linked hashmap and its features?

0 Answers  


explain the concept of inheritance with an example?

9 Answers   Polaris,


How do you bind variables?

0 Answers  


What restrictions are placed on method overloading in java programming?

0 Answers  


What is passed by reference and pass by value ?

0 Answers  


what is purpose of writting public static void main(Strind arg[]) in java..?

4 Answers   ITC Infotech,


Categories