What is the best way to release memory allocated by an array of bytes (new byte[size] in Java)?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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.
When creating a new byte[] in Java, you do something like
To free it, you should do
If something else references your byte array, like
you need to also set the other references to null, like so
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.
Stop referencing it.