Free memory of a byte array in Java Free memory of a byte array in Java arrays arrays

Free memory of a byte array in Java


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.


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