views:

290

answers:

4

What is the best way to release memory allocated by an array of bytes (new byte[size] in Java)?

+15  A: 

Stop referencing it.

Alexander Pogrebnyak
It brought a smile on my face :)
BalusC
Java object == existential crisis "If no one is referencing me do I still exist?"
M. Jessup
Leave all the poor pieces of memory alone. Stop referencing them. Now :)
schnaader
+2  A: 

Removing all the reference to that array of bytes. The garbage collector will take care of the rest.

HoLyVieR
+1  A: 

When creating a new byte[] in Java, you do something like

byte[] myArray = new byte[54];

To free it, you should do

myArray = null;

If something else references your byte array, like

yourArray = myArray;

you need to also set the other references to null, like so

yourArray = null;

In Java garbage collection is automatic. If the JVM can detect that a piece of memory is no longer reachable by the entire program, then the JVM will free the memory for you.

Edwin Buck
The JVM will free the memory when it feels like it. As a first order approximation for almost every java program, don't worry about memory, be happy, the JVM's got your back.
GregS
Generally, you shouldn't need to set a reference to null to allow the object to be collected. If you need to do this there is very likely a serious problem with your implementation IMHO.
Peter Lawrey
@Peter: Depends. If you know that the variable is going to be living quite a bit longer than the array it was referring to, then setting it explicitly makes sense. That's fairly rare though.
Donal Fellows
@Donal only if its large and you need the memory again. If you have two large pieces of work, you are better off breaking the code into the piece which uses the byte[] and the code which doesn't into two methods. That way there is no byte[] to clear as such and is likely to make your code more readable.
Peter Lawrey
@Peter: Indeed. Variable lifetime is usually easier (and not just in Java). I did note that it was a fairly rare thing to do. :-)
Donal Fellows
One place I would use it is in unit tests. Often a unit test framework retains all the tests performed as Objects. This can mean you run out of memory needlessly when all that needs to be retained is the result of each test. If you are running out of memory performing many tests, you might find you need to clear fields when the test completes. (Or use local variables instead)
Peter Lawrey
I agree, most of the time the varible passes out of scope and setting it to null is unnecessary; however, remembering that this person is just learning JVM garbage collection, setting the variable to null explicitly drives the point home.
Edwin Buck
A: 

Setting all references to it to null will make it a candidate for Java's automatic garbage collection. You can't be sure how long it will take for this to happen though. If you really need to explicitly reclaim the memory immediately you can make a call to System.gc();

Also just to clear you may not need to set the references to null explicitly. If the references go out of scope they are automatically nulled e.g. a local variable reference will be nulled once the method it is declared in finishes executing. So local variables are usually released implicitly all the time during an apps runtime.

Alb
System.gc() does not reclaim memory immediately. You're just telling the JVM "Please, if you feel like it, maybe you can clean stuff up." It's just a suggestion.
Jonathon
System.gc() won't force a GC. It's merely a suggestion. It's like the difference between your roommate saying "dude, this place is a wreck, what's up with that?" and your mother saying "CLEAN UP THIS INSTANT!" In most implementations, System.gc() is merely your roommate.
Jim Kiley
@Jonathon @Jim I wasn't aware of that. The javadocs say "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." and in my experience of calling it it has appeared to run immediately, but maybe I'm wrong
Alb