views:

684

answers:

4

Is there a way to free memory in Java, similar to C's free() function? Or is setting the object to null and relying on GC the only option?

+6  A: 
System.gc(); 

Runs the garbage collector.

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.

Not recommended.

Dean J
+1 for the last line.
musicfreak
Commenting on my own post, this often doesn't do anything, and calling it repeatedly can cause the JVM to become unstable and whatnot. It may also run over your dog; approach with caution.
Dean J
-1, because this does NOT run the garbage collector. It only SUGGESTS to the system that it might be a good moment to run the garbage collector. But the garbage collector is so sophisticated that the programmer can't really know better when a good time is for GC.
Jesper
I would put heavy emphasis on the "suggests" part of "Calling the gc method suggests that the JVM expand effort"
matt b
Note also that a single invocation of `System.gc()` usually collects just a little bit of garbage. To collect everything, you need to invoke it in a loop, using `Runtime.freeMemory()` to monitor when the amount of free memory no longer increases.But calling `System.gc()` explicitly is indeed rarely needed. One case where it's useful is when you're trying to measure memory consumption, since a GC run in the middle of that will mess up your measurements.
jk
@Jesper, Dean's answer states "suggests". In fact he posted the exact documentation from the method's javadocs...
matt b
@Dean J: You might want to blockquote your text to make it clear that you're quoting directly from the Java documentation.
Daniel Pryden
@Daniel: Rather than tell him to in a comment, it would've been quicker and more useful to just edit the answer yourself and add the single '>' that it required.
Software Monkey
Fixed, as everything I cut-and-pasted wasn't previously blockquoted.
Dean J
@Software Monkey: Yes, I could have just edited it. But since Dean J was obviously active (posting only a few minutes ago), I figured it was a courtesy to ask him to do it. If he hadn't, I would have come back here and made the edit and deleted my comment.
Daniel Pryden
It would also we worth saying WHY it is not recommended. If the JVM pays attention to the "suggestion" to run the GC, it will almost certainly make your app run slower, possibly by many orders of magnitude!
Stephen C
Per heavy feedback, I've also bolded SUGGESTS. Hopefully that's clearer now.
Dean J
+7  A: 

Java uses managed memory, so the only way you can allocate memory is by using the new operator, and the only way you can deallocate memory is by relying on the garbage collector.

This memory management whitepaper (PDF) may help explain what's going on.

You can also call System.gc() to suggest that the garbage collector run immediately. However, the Java Runtime makes the final decision, not your code.

According to the Java documentation,

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.

Daniel Pryden
`System.gc()` **DOES NOT** force the garbage collector to run. It's only a suggestion.
Thomas Owens
@Thomas Owens: Correct. I've clarified this in my most recent edit.
Daniel Pryden
It does force the Garbage Collector to run. It does not force it to free memory though...
Pablo Santa Cruz
No Pablo, it does not force the GC to run.
Jesper
OK. I see. Thanks for pointing that out.
Pablo Santa Cruz
Thanks, first paragraph was pretty much what I wanted to know. Thanks for clarifying everything and yes, I understood, I won't be using System.gc() :)
Felix
I've been told by a very reliable person that all HotSpotVM's garbage collectors ignore `System.gc()` entirely.
Esko
+5  A: 

No one seems to have mentioned explicitly setting object references to null, which is a legitimate technique to "freeing" memory you may want to consider.

For example, say you'd declared a List<String> at the beginning of a method which grew in size to be very large, but was only required until half-way through the method. You could at this point set the List reference to null to allow the garbage collector to potentially reclaim this object before the method completes (and the reference falls out of scope anyway).

Note that I rarely use this technique in reality but it's worth considering when dealing with very large data structures.

Adamski
If you really are doing alot of work on an object which is only used for part of a method I suggest either; your method is too compilicated, break the method into the before and after portions, or use a block for the first half of code (the later is more useful for test scripts)
Peter Lawrey
+1  A: 

If you really want to allocate and free a block of memory you can do this with direct ByteBuffers. There is even a non-portable way to free the memory.

However, as has been suggested, just because you have to free memory in C, doesn't mean it a good idea to have to do this.

If you feel you really have a good use case for free(), please include it in the question so we can see what you are rtying to do, it is quite likely there is a better way.

Peter Lawrey