File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes The Java Garbage Collector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The Java Garbage Collector" Watch "The Java Garbage Collector" New topic

The Java Garbage Collector

Iliyan Ivanov

Joined: Jun 30, 2011
Posts: 8
Can you please explain to me in short what tha Garbage Collector does and why is it useful ?
Thank you
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146


GC is one that do automatically recollects the memory if no valid reference is pointing to it.

It frees the overhead of programmer to recollect the memory programatically.

Its the part of JVM


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
It prevents you running out of memory, unless you do something daft.
Kalyan Naveenan

Joined: Jul 23, 2011
Posts: 12
Iliyan Ivanov wrote:Can you please explain to me in short what tha Garbage Collector does and why is it useful ?
Thank you

Helps you clear the unwanted/unnecessary memory occupied. In short doesn't let you run out of memory.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Each object that you create in your Java program takes up some memory. The Java virtual machine keeps track of what part of the computer's memory is occupied by objects and what part is free (ready to store new objects in).

In a programming language such as C++, you are required not only to allocate, but also to free the memory that you have allocated. When you are programming in C++, it can easily happen that you forget to free the memory of some object that you had allocated previously. Especially if the program becomes larger and more complicated, you can easily overlook that you should have freed some memory somewhere. When there are errors like that in a program, the program has memory leak: some block of memory is allocated and never freed.

Memory leaks can become a big problem, especially in long-running applications, where the amount of memory that the program has allocated grows and grows over time, slowing the whole system down, until eventually the program crashes because the computer doesn't have any more memory left to allocate.

In Java, this has been made much easier than in C++. You can create new objects, but you never have to free them yourself - the JVM frees objects automatically when they are not used anymore. The garbage collector is the process in the JVM that periodically frees the memory allocated for objects that are not used anymore.

Java's automatic memory management is a big advantage over C++, because you almost never have to worry about freeing objects, which means you can write your program in less time (you can be more productive) and the whole problem of memory leaks isn't there.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: The Java Garbage Collector
It's not a secret anymore!