简体   繁体   English

Java:将Array设置为null以释放内存

[英]Java: Setting Array to null to free memory

I have an Android application where I allocate large arrays, fill them up, and throw them into buffers. 我有一个Android应用程序,在其中分配大数组,将它们填满,然后将它们放入缓冲区。 After doing this, the arrays are lying around, doing nothing. 完成此操作后,阵列将摆在周围,什么也不做。 Would setting them to null theoretically improve the performance of my app, or is the effect negligible/nonexistent? 从理论上将它们设置为null会提高我的应用程序的性能,还是效果可以忽略/不存在?

Generally no, setting the array to null will not improve performance or memory use in any way. 通常不,将数组设置为null不会以任何方式提高性能或内存使用。

The garbage collector will free up the memory when it decides that the object is no longer accessible. 当垃圾回收器确定该对象不再可访问时,它将释放内存。 Explicitly setting a reference to null is a good signal to the garbage collector that the object is free-able, but if those arrays are already no longer being referenced anyway (because, say, their containing object is no longer referenced) then explicitly setting it to null won't make the garbage collection happen any more quickly. 显式地将引用设置为null可以很好地向垃圾回收器表明该对象是可释放的,但是如果这些数组已经不再被引用(例如,不再包含其包含的对象),则可以对其进行显式设置为null不会使垃圾回收更快地进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM