取消引用数组中的对象以进行 Java 垃圾收集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14790314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:24:59  来源:igfitidea点击:

Dereferencing Objects in an Array for Java Garbage Collection

javaarraysgarbage-collectiondereference

提问by TIER 0011

I have done some research on the java garbage collector and understand that an object who is no longer referenced will/should be handled by the garbage collector. In terms of arrays-of-objects, I am aware that assigning a new object to a location in the array does not properly deallocate the previously allocated object.

我对 java 垃圾收集器进行了一些研究,并了解不再引用的对象将/应该由垃圾收集器处理。就对象数组而言,我知道将新对象分配给数组中的某个位置并不能正确释放先前分配的对象。

  1. I would like to know how to remove and properly deallocate an object from an array at location x and assign a new object to the same array at location x.
  2. I would also like to know how to properly deallocate the array itself.
  1. 我想知道如何从位置 x 的数组中删除并正确释放对象,并将新对象分配给位置 x 的同一个数组。
  2. 我还想知道如何正确释放数组本身。

回答by Hyman

Setting an object in an array to nullor to another objects makes it eligible for garbage collection, assuming that there are no references to the same object stored anywhere.

null假设没有对存储在任何地方的同一个对象的引用,将数组中的一个对象设置为另一个对象使其符合垃圾回收的条件。

So if you have

所以如果你有

Object[] array = new Object[5];
Object object = new Object() // 1 reference
array[3] = object; // 2 references
array[1] = object; // 3 references


object = null; // 2 references
array[1] = null; // 1 references
array[3] = new Object(); // 0 references -> eligible for garbage collection

array = null; // now even the array is eligible for garbage collection
// all the objects stored are eligible too at this point if they're not
// referenced anywhere else

The garbage collection rarely will reclaim memory of locals though, so garbage collection will mostly happen already outside of the scope of the function.

垃圾收集很少会回收本地人的内存,因此垃圾收集主要发生在函数范围之外。

回答by Mikhail Vladimirov

In java you don't need to explicitly deallocate objects, regardless of whether they are references from array, or simple field or variable. Once object is not strongly-reachable from root set, garbage collection will deallocate it sooner or later. And it is usually unreasonable to try helping GC to do its work. Read this articlefor more details.

在 java 中,您不需要显式释放对象,无论它们是来自数组的引用,还是简单的字段或变量。一旦对象不能从根集强可达,垃圾收集迟早会释放它。并且试图帮助 GC 完成它的工作通常是不合理的。阅读这篇文章了解更多详情。

回答by yams

Object De Allocation should occur upon closing or opening by nulling out the object. JUnit tests do assertGC on objects to make sure all objects have been garbage collected. Java will not garbage collect an item if it has a reference. I would strongly recommend doing JUnit testing.

对象解除分配应该在关闭或打开对象时发生。JUnit 测试对对象执行 assertGC 以确保所有对象都已被垃圾回收。如果项目有引用,Java 将不会对其进行垃圾回收。我强烈建议进行 JUnit 测试。

回答by M Sach

Well all objects will be collected by Garbage collector automatically when they are no longer referenced. You dont have to woory about it. Still if you want you can assign null to array and on the next run of GC, it memory which was allocated to array will be deallocated itself. For running GC explicitly, you can do

好吧,当不再引用所有对象时,垃圾收集器将自动收集它们。你不必担心它。尽管如此,如果您愿意,您可以将 null 分配给数组,并且在下一次 GC 运行时,分配给数组的内存将自行释放。要显式运行 GC,您可以执行

    java.lang.Runtime.getRuntime().gc();