What is garbage collection in Java, and how can it be used ?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
What is difference between throw and throws ?
What is the static field modifier?
how are methods defined?
Can extern variables be initialized?
how to call a method in different package?
what is the purpose of method overriding in java where v r completely re-defining a inherited method instead y can't v create a new method and define.If the question is very silly plz excuse me and do reply.Thank U!
7 Answers emc2, Kumaran Systems,
What do you mean by light weight and heavy weight components?
Can you make a constructor final?
how can we use the servlet as standalone apllication?should we need to extend any class?
how a programmer confirms that the data submitted has been succesfully inserted into the database(either oracle or my sql).. How a programmer confirm if there is any problem with the program he wrote for insertion
Can a main method be declared final?
what is the difference between AWT and SWING what is the advantage of using swing?