• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Garbage Collection

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Are there any downsides to using the System.gc(); command in your program, for instance when you create an object (say socket) and are finished calling that object (after socket.close() . You may still create other instances of the object (say socket2).
This may be a simple question, but I really need to know before I liberally sprinkle my code with System.gc();

Thanks,
-Deb
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best time to call System.gc is when you are pretty sure the system won't be called on to do something for a while. That is easy with GUI applications where you know the user is going to be reading the screen but I don't know if this applies to your problem. I have done what you are talking about with a server application but I don't know if it is optimum.
Bill
 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also know that calling the garbage collector is NOT a guarantee that it will actually do anything. The API docs state this:
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
In other words, all you can do is "suggest" it run.
 
Debbie Argulkar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for the prompt and helpful replies.
I do have a GUI which should start and stop the data transmission (from db) by a socket.
Thanks,
-Deb
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Debbie Argulkar:
This may be a simple question, but I really need to know before I liberally sprinkle my code with System.gc();


Why would you want to? I *never* found it to be necessary to do something like that...
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me add that there is one and only one guarantee you get about garbage collection, and that is that it will run before the JVM throws an OutOfMemoryError at you.
This means that the only valid reason to call System.gc() is that you know there's lots of garbage on the heap[1], you have a natural "lull" in your application, and not collecting garbage at that point will impact performance shortly after. Even so, you don't get any guarantees that the garbage collector will run.
- Peter
[1] With modern generational garbage collectors, I think this mainly happens when you release lots of objects that you've held on to for a fairly long time. Objects with a short lifespan live on a kindergarten heap which is garbage collected very aggressively. If you're interested, you might enjoy this article.
[ February 15, 2003: Message edited by: Peter den Haan ]
 
I once met a man from Nantucket. He had a tiny ad
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic