• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Explicit Garbage Collection invocation

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we explicitly call Garbage collector , then it will reove memory for non-referenced objects Or it only removes once the managed memory is low(the memory which is used by running program)?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If we explicitly call Garbage collector



Even if you call System.gc(), it is not guaranteed that the garbage collector will run, it will take its own sweet time to run. Call to System.gc() is just a request to the JVM to run the the garbage collector as soon as possible.
However, if the garbage collector runs, it will surely clean all the non-refernced memory from the JVM.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is really no reason to ever invoke garbage collection. The JVM is much smarter than you (not "you" specifically, but "you" the whole world, just about). It knows when it needs to do garbage collection, and most of the time, it will just ignore your request anyway.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Fred and nitesh.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that it is gauranteed garbage collector would always remove all non-referenced objects. I don't see any reason to have a explicit gc call. GC is smart enough to handle that.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam, is there any specific reason as to why the garbage collector will not clean all the non-referenced objects that it finds while it is running?
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Anupam, is there any specific reason as to why the garbage collector will not clean all the non-referenced objects that it finds while it is running?


Do you ever remove plants from your garden which can be fruitful!
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam,

Though we call gc call,JVM should realize that there is a need to do that;

we can create a situation like
1)see free space using Runtime class
2)try to occupy the entire free space by creating loop as,

for (int i=0;i<= ??? ;i++) \\ ?? depends on free space
Double d = new Double(225);


3) See free space again

4)then call gc.

5) Now see the free space

I am sure .. you will regain the free space.
[ March 20, 2007: Message edited by: Srinivasan thoyyeti ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is surely not required to be known for the SCJP version atleast not till 1.5

First there are a number of garbage collector implementation that a JVM can support. Secondly JVM's generally would not actually collect all non referenced objects in one go, though of course it can. JVM would particularly look for objects with high infant mortality rate(objects that die soon after being allocated). You can and probably should read this for more info on Garbage collection.

Calling gc explicitly would cause the garbage collector to scan the entire memory and then sweep the non referenced objects. Which might not be required. So don't use it unless there's a specific reason to use it.

Here's a program which benchmarks the performance with constant gc calls and one with no explicit gc calls.



Even if you remove the GC from the loop and keep it outside the loop then also there would be a considerable difference.
[ March 20, 2007: Message edited by: Anupam Sinha ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam,

Garbage collector is not meant for improving your system performance in terms of time but in terms of space, by deallocating unused memory occupied by Objects.

You should check for free space rather than time needed to execute statements.

[ March 20, 2007: Message edited by: Srinivasan thoyyeti ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't wanna argue but how come performance is function of free space?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anupam,

We should have perfect balance Time and Space to get performance.

Garbage collector helps us to get rid of performance problems caused by "lack of space".

if you have still doubt,
Please search in google with "Time Space trade off".
if you have read "Data Structures" before you can understand it easily.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Singh:

Do you ever remove plants from your garden which can be fruitful!


Plants without roots need not be removed, they just fall down!
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely when I said gc shouldn't be called explicitly I did not mean that let the process die due to lack of memory. What I wanted to emphasize was that an explicit gc call is not required for and gc is generally smart enough to figure out when to gc.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

explicit gc call is not required for and gc is generally smart enough to figure out when to gc



Absolutely correct . GC is smart enough. But explicit call to Systme.gc() always makes chance for GC to become more smart

Here is the code suggested by srinivasan



Here the explicit call System.gc() makes GC more smart
and gives us more free space. If we very much concern about memory free space, explicit GC call will make difference.
[ March 21, 2007: Message edited by: JB Ramesh ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly my point for not using explicit gc. In JB's code snippet if we remove the call to gc and measure the time there would be a difference in explicit call invocation and if we remove the call. Secondly we are not making gc smarter by explicitly calling gc. In your simple program the code would generally complete much earlier if you remove the explicit calls. I mean what would you do with the free memory when you do not need it?
 
JB Ramesh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Secondly we are not making gc smarter by explicitly calling gc. In your simple program the code would generally complete much earlier if you remove the explicit calls.



Correct. you are concerning about the performance. But Garbage collection deals with JVM Memory.


I mean what would you do with the free memory when you do not need it?



Yes. when we dont need free space, we dont need to worry about GC. What about when you need it ? if we consider Data and memory, it makes difference. Consider about data structures and manupulation on data. If a program constructs huge number of objects and operates on that, then memory plays important role there. Ther we can optimize the program to address problem with free space and memory leake by using GC.

[ March 21, 2007: Message edited by: JB Ramesh ]
[ March 21, 2007: Message edited by: JB Ramesh ]
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic