简体   繁体   English

为什么此String不符合垃圾回收的条件?

[英]Why this String is not eligible for garbage collection?

The following code creates one array and one string object. 以下代码创建一个数组和一个字符串对象。 How many references to those objects exist after the code executes? 代码执行后,对这些对象有多少引用? Is either object eligible for garbage collection? 对象是否有资格进行垃圾回收?

...
String[] students = new String[10];
String studentName = "Peter Parker";
students[0] = studentName;
studentName = null;
...

My answer was studentName is eligible for garbage Collection.But the answer given was both are not eligible.What I thought was students[0] refers to the String "Peter Parker" and studentName also does the same.Now that studentName refers to null, students[0] remains referring to "Peter Parker" ( I checked this by printing it out).The explanation given was students[0] is still referring to studentName so studentName is also not eligible for garbage Collection.But I'm not understanding this that since studentName now refers to null and students[0] refers to "Peter Parker".Is my understanding wrong? 我的回答是studentName有资格进行垃圾收集。但是给出的答案都不符合条件。我认为学生[0]指的是字符串“Peter Parker”,studentName也是这样。现在studentName指的是null,学生[0]仍然指的是“Peter Parker”(我通过打印出来检查了这一点)。给出的解释是学生[0]仍然指的是studentName所以studentName也没有资格进行垃圾收集。但我不理解因为studentName现在指的是null而学生[0]指的是“Peter Parker”。我的理解是错的吗?

Before studentName = null; studentName = null;之前studentName = null; is executed, studentName and students[0] both hold references to the same String object (whose value is "Peter Parker" ). 执行时, studentNamestudents[0]都持有对同一String对象(其值为"Peter Parker" )的引用。 When you assign null to studentName , students[0] still refers to that object, so it can't be garbage collected. 当您为studentName指定nullstudentName students[0]仍然引用该对象,因此无法进行垃圾回收。

Java doesn't garbage collect reference variables, it garbage collects the objects that were referenced by these variables once there are no more references to them. Java不会垃圾收集引用变量,一旦没有对这些变量的引用,它就会收集这些变量引用的对象。

Here String object is created with "Peter Parker" and studentName is just referring it. 这里String对象是用"Peter Parker"创建的, studentName只是引用它。 Java will garbage collecting those object who are lost and have no reference , here you are confused with reference studentName . Java将垃圾收集那些丢失并且没有引用的对象,在这里你与参考studentName混淆。 by assigning null to studentName you are just removing one reference of "Peter Parker" ; 通过为studentName指定null ,您只需删除一个"Peter Parker"引用; but it still refer by array element , so it will not garbage collected. 但它仍然由数组元素引用,所以它不会被垃圾收集。

How many objects do you have? 你有多少个物品?

You have two objects: 你有两个对象:

  • The string "Peter Parker" 字符串"Peter Parker"
  • An array of strings. 一串字符串。

How many references are/were there to those objects? 这些对象有多少引用?

There were three references: 三个参考:

  • The variable studentName is referencing the string object. 变量studentName正在引用字符串对象。
  • The variable students is referencing the array object. 变量students正在引用数组对象。
  • The first position of the array object, students[0] is also referencing the string object. 数组对象的第一个位置, students[0]也引用了字符串对象。

How many references to those objects exist after the code executes? 代码执行后,对这些对象有多少引用?

After executing this line: 执行此行后:

studentName = null;

Only the first of the three references has been deleted. 只删除了三个引用中的第一个。 We still have two references left. 我们还剩下两个参考文献。

Is either object eligible for garbage collection? 对象是否有资格进行垃圾回收?

No , because: ,因为:

  • The array object is still referenced by the students variable. 数组对象仍然由students变量引用。
  • The string object "Peter parker" is still referenced by the first position of the array object, students[0] . 字符串对象"Peter parker"仍然由数组对象的第一个位置students[0]引用。

Since both objects are still referenced somewhere, none of them are eligible for garbage collection. 由于两个对象仍在某处引用,因此它们都不符合垃圾回收的条件。


PS: I've used generic "string" and "array" terms for the objects on purpose, so it's easier to mentally dissociate them from the actual String and String[] variables in the code. PS:我有意为对象使用通用的“字符串”和“数组”术语,因此更容易在心理上将它们与代码中的实际String和String []变量分离开来。

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

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