views:

779

answers:

8

Hello there,

Please anyone can tell me if I can force garbage collection in java anyway? Even it was tricky to do. I know about System.gc(); and Runtime.gc(); but they don't force, they suggest to do GC, but actually no force occurs. Please I need that horribly.

Best Regards,

Tarek,

+4  A: 

Your best option is to call System.gc() which simply is a hint to the garbage collector that you want it to do a collection. There is no way to force and immediate collection though as the garbage collector is non-deterministic.

Andrew Hare
A: 

It would be better if you would describe the reason why you need garbage collection. If you are using SWT, you can dispose resources such as Image and Font to free memory. For instance:

Image img = new Image(Display.getDefault(), 16, 16);
img.dispose();

There are also tools to determine undisposed resources.

Paul Lammertsma
+2  A: 

If you need to force garbage collection, perhaps you should consider how you're managing resources. Are you creating large objects that persist in memory? Are you creating large objects (e.g., graphics classes) that have a Disposable interface and not calling dispose() when done with it? Are you declaring something at a class level that you only need within a single method?

Bob Kaufman
A: 

As a general rule, it is a bad idea to force garbage collection. If we ignore virtual memory effects, garbage collection works most efficiently when there is lots of garbage to reclaim; i.e. when the allocator has run out of immediately usable free memory. If you force the garbage collector to run at any other time it will spend more time (on average) collecting.

Stephen C
+1  A: 

Under the documentation for OutOfMemoryError it declares that it will not be thrown unless the VM has failed to reclaim memory following a full garbage collection. So if you keep allocating memory until you get the error, you will have already forced a full garbage collection.

Presumably the question you really wanted to ask was "how can I reclaim the memory I think I should be reclaiming by garbage collection?"

Pete Kirkham
A: 

.gc is a candidate for elimination in future releases - a Sun Engineer once commented that maybe fewer than twenty people in the world actually know how to use .gc() - I did some work last night for a few hours on a central / critical data-structure using SecureRandom generated data, at somewhere just past 40,000 objects the vm would slow down as though it had run out of pointers. Clearly it was choking down on 16-bit pointer tables and exhibited classic "failing machinery" behavior.

I tried -Xms and so on, kept bit twiddling until it would run to about 57,xxx something. Then it would run gc going from say 57,127 to 57,128 after a gc() - at about the pace of code-bloat at camp Easy Money.

Your design needs fundamental re-work, probably a sliding window approach.

Nicholas Jordan
I have something like that, a lot of objects in memory I can't deallocate them. OutOfMemory exception is thrown, I want to Force GC to test if there is some Infinite Object creation process or these objects are the one used by my system.
elkaund
Sounds like you are working on the same problem I am, pls explain: "Infinite Object creation" ... good research project, maybe you can post a question or something in a java area here ( I'm sort new here and to not know the "Finite Automa" of how the site works ) I tried yesterday and ended up doing file.dat as the compiler complained "too much code" on 40,000 base36 BigIntegers coded as a static final String[] I'm gonna stick my neck here and speculate that the entire JVM is limited on 16-bit pointers, I bet what we have to do is agressively null and read in from disk ...
Nicholas Jordan
Really, I don't get you. But to be clear about "Infinite Object Creation" I meant that there is some piece of code at my big system do creation of objects whom handles and alive in memory, I could not get this piece of code actually, just gesture!!
elkaund
A: 

The best (if not only) way to force a GC would be to write a custom JVM. I believe the Garbage collectors are pluggable so you could probably just pick one of the available implementations are tweak it.

Note: This is NOT the easy answer.

Mainguy
A: 

Really, I don't get you. But to be clear about "Infinite Object Creation" I meant that there is some piece of code at my big system do creation of objects whom handles and alive in memory, I could not get this piece of code actually, just gesture!!

This is correct, only gesture. You have pretty much the standard answers already given by several posters. Let's take this one by one:

  1. I could not get this piece of code actually

Correct, there is no actual jvm - such is only a specification, a bunch of computer science describing a desired behaviour ... I recently dug into initializing Java objects from native code. To get what you want, the only way is to do what is called aggressive nulling. The mistakes if done wrong are so bad doing that if it is done wrong that we have to limit ourselves to the original scope of the question:

  1. some piece of code at my big system do creation of objects

Most of the posters here will assume you are saying you are working to an interface, if such we would have to see if you are being handed the entire object or one item at a time.

If you no longer need an object, you can assign null to the object but if you get it wrong there is a null pointer exception generated. I bet you can achieve better work if you use NIO

Any time you or I or anyone else gets: "Please I need that horribly." it is almost universal precursor to near total destruction of what you are trying to work on .... write us a small sample code, sanitizing from it any actual code used and show us your question.

Do not get frustrated. Often what this resolves to is your dba is using a package bought somewhere and the original design is not tweaked for massive data structures.

That is very common.

Nicholas Jordan