25

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

1
  • 7
    re-use it. Best de-allocation ever. Commented Feb 6, 2011 at 22:46

4 Answers 4

63

Stop referencing it.

Sign up to request clarification or add additional context in comments.

5 Comments

It brought a smile on my face :)
Java object == existential crisis "If no one is referencing me do I still exist?"
Leave all the poor pieces of memory alone. Stop referencing them. Now :)
will all references be removed when the method ends or setting variable = null necessary
@ir2pid Array will become eligible for garbage collection when variable holding it goes out of scope, e.g. when method call ends. This means you don't need to set variable to null
18

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.

9 Comments

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.
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: 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 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: Indeed. Variable lifetime is usually easier (and not just in Java). I did note that it was a fairly rare thing to do. :-)
|
6

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

Comments

3

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.

4 Comments

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.
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.
@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
but in practice it runs the Garbage collector. Yes there are options to prevent this, but if you haven't gone looking for these it'll will just run the thing..no questions asked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.