Removing an object in java

The following code creates one array and one string object.

Now my questions

  • How many references to these objects exist after code execution?
  • Why?

Here is my code

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

I thought the answer is only one object, i.e. students
But according to Oracle docs , Neither object is eligible for garbage collection

How do I make a conclusion?

+4
source share
6 answers

How many references to these objects exist after code execution?

  • One reference to String[] , which can be obtained through the students expression.
  • One reference to String , which can be obtained through the expression students[0] .

Why?

Essentially, the answer is that objects, not variables , may have the right to garbage collection.

In your case, you copied the link of the string (which is the object) into the first slot of the array. Even after you clear (set to null ) the initial variable, the same object is still displayed from your code with a different name (and by β€œname” here I mean β€œexpression”, as described above). This is why the string is still not suitable for garbage collection.


For comparison, consider your code without the third line:

 String[] students = new String[10]; String studentName = "Peter Smith"; studentName = null; 

In this case, the string "Peter Smith" really has the right to collect garbage, as one would expect, because it can no longer be received by any expression.


(All of the above refers to the Java language and leaves any possible JVM optimizations aside.)

+10
source
 String[] students = new String[10]; // 1. One object (array of String, 1 reference to it - students) String studentName = "Peter Smith"; // 2. Two objects (array from (1), string 'Petter Smith'), two references // (students from (1), studentName that refers String object ) students[0] = studentName; // 3. Two objects (the same as (2)), 3 references ( in addition to references (2) // first element of students array refers to the same object as studentName studentName = null; // Two objects (Array and String "Peter Smith"), two references (one is array, // another is students[0] // Neither of them can be marked for garbage collection at this point // (Array and String "Peter Smith" ) 

Hope this makes sense.

+4
source

Here "Peter Smith is an object and is assigned to students[0] , so it cannot be garbage collected and studentName=null , it points to nothing, so no attempt to garbage collect it.

Thus, both debris cannot be collected.

+3
source

An object can have multiple references, even if you set the studentName reference to zero, the String object still refers to student[0] . Thus, the string "Peter Smith" cannot be garbage collected.

+3
source

You are right that students is an object, but it is an object of type String array. In the array, each element of the array can refer to other objects, and the first element, students [0], refers to a string object containing "Peter Smith", so the object is still referenced and therefore has no right to garbage collection. When students go out of scope and become available to the GC itself, then the row object will also be.

+1
source

students created and then modified, so there is no reason to collect it, because we have a link. studentName is created, then the link is assigned to students , so when it is dereferenced on the last line, the reference to the object, the string, still exists in the array, so nothing is collected by the GC.

+1
source

Source: https://habr.com/ru/post/1484035/


All Articles