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

- 44,836
- 10
- 105
- 121
-
12
-
31Java object == existential crisis "If no one is referencing me do I still exist?" – M. Jessup Jun 04 '10 at 13:01
-
Leave all the poor pieces of memory alone. Stop referencing them. Now :) – schnaader Jun 04 '10 at 15:23
-
will all references be removed when the method ends or setting variable = null necessary – ir2pid Jun 28 '17 at 11:42
-
1@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` – Alexander Pogrebnyak Jul 05 '17 at 21:33
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.

- 69,361
- 7
- 100
- 138
-
3The 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. – President James K. Polk Jun 04 '10 at 13:10
-
4Generally, 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 Jun 04 '10 at 14:49
-
3@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 Jun 04 '10 at 15:24
-
1@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 Jun 05 '10 at 07:55
-
1@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 Jun 05 '10 at 16:12
-
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 Jun 05 '10 at 20:39
-
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 Jun 07 '10 at 05:53
-
After garbage collection, are the bytes stored in the memory location zeroized? Say, immediately after garbage collection, if I take a process level memory dump, will I still find the same data in the same memory location held by the byte array or will the memory location contain zeroes? – MediumOne Feb 24 '18 at 12:16
-
@MediumOne Generally no; but, there is more than one garbage collector available, so it depends. The default garbage collectors don't. If you want memory zeroing, you could set the bytes to zeros before dereferencing, or implement your own garbage collector. Of course to dump the ram, you'd have to be the user, or the superuser. If you're the latter, you can spy on programs by a large number of means. Explicit zeroing is the best approach, as it minimizes the time the secret is in RAM – Edwin Buck Feb 24 '18 at 14:36
Removing all the reference to that array of bytes. The garbage collector will take care of the rest.

- 10,985
- 5
- 42
- 67
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.

- 3,601
- 6
- 33
- 38
-
4System.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 Faust Jun 04 '10 at 15:22
-
3System.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 Jun 04 '10 at 15:23
-
@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 Jun 04 '10 at 15:44
-
1but 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 – Gareth Davis Dec 04 '10 at 23:16