• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collector  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai everyone. I am new to this forum. Can anyone tell me about the usage of garbage collector in Java
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Whenever any code is executed, multiple objects are created (either in the code you write, whenever you use the new keyword) or by the JVM. Obviously these objects consume memory. To optimize things, the JVM will free the memory of objects which are no longer required. Think of it as a scenario where you keep buying the latest handsets, and the old handsets are no longer of any interest to you. This process of freeing memory is called garbage collection.
It is interesting to note that the process is totally decided by the JVM and will kick in if and only if the JVM thinks there is a need for garbage collection. (e.g. when the allocated memory is not going to be sufficient to further run the code)

If you have any other specific questions besides this, please do feel free to ask.

This is a beginners topic, and I will move this question over to the beginners forum for you
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote: . . . a scenario where you keep buying the latest handsets, and the old handsets are no longer of any interest to you. . . .
Welcome again

But, as Maneesh has hinted, it is a scenario where you only throw out the old handsets when the drawer is full. Garbage collection (=GC) usually only occurs when the heap is nearly full. You can try to run it with System.gc() but that might not cause a GC cycle to occur, or only an incomplete GC cycle. Automatic GC is much more accurate than programmer‑directed object deletion.
You now know all that you actually need to know about GC.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!