• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The Java Garbage Collector

 
Iliyan Ivanov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain to me in short what tha Garbage Collector does and why is it useful ?
Thank you
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50172
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It prevents you running out of memory, unless you do something daft.
 
Kalyan Naveenan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic