java - Confused about garbage collection -


How many object lines 7 are eligible for garbage collection after performing the following code? In my opinion there is 1 object which is z. What is right?

  public class tester {public static zero main (string [] args) {integer x = new integer (3000); Integer y = new integer (4000); Integer z = new integer (5000); Object a = x; X = y; Y = z; Z = Null; // line 7}}   

Thanks a lot.

Do not confuse context with an object Object is a real object that is created, reference is only one There is a name that refers to it.

You have created three objects, let them do 3000 , 4000 and 5000 . You have also set up references as follows:

  Riff object --- ------ x - & gt; 3000 y - & gt; 4000Z - & gt; 5000   

After the work, you end on:

  Riff object ------ A-> 3000 x - & gt; 4000 y - & gt; 5000 z   

Therefore, any of the items is not subject to garbage collection. Each one is still a reference to it.


On the contrary, if you want to remove this line:

  object a = x;   

Then the result of the assignment will be:

  the riff object ------- 3000x - & gt; 4000 y - & gt; 5000 z   

and object which we call 3000 will be eligible for garbage collection, because now you have no access to it Not the way.


And, on one hand, you might want to consider the fact that automatic waste collection was created, one of the main reasons is to dispute these types of questions: - ) Generally (although there are definitely exceptions), you should not need to worry about things.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -