IntelliJ Java IDE
The moose likes Performance and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Debbie Argulkar
Ranch Hand

Joined: Jul 26, 2002
Posts: 43
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 11862
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
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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

Joined: Jul 26, 2002
Posts: 43
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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 ]
 
 
subject: Garbage Collection
 
Threads others viewed
Object for GC....
Garbage Collection - Boone's Mock Q
Can u help me?
applet within browser not releasing file resource
Garbage collection Q
developer file tools