0

This is a question I have from Lecture about the discussion of Garbage Collection. I cannot figure which line cause the garbage collection.

I have tried using the compiler to run the programming and the outputs are

#1: [C001, C002]
#2: [C001, C002, C001]
#3: [C001, C002, C001]

I know that there is a line that cause Garbage Collection but I cannot figure it out. At the same time, professor required us to draw a diagram showing how the objects and data, lists go.

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

        Customer a = new Customer("C001");
        Customer b = new Customer("C002");

        ArrayList<Customer> list1 = new ArrayList<Customer>();
        list1.add(a);
        list1.add(b);
        System.out.println("#1: " + list1.toString()); Icdddd

        ArrayList<Customer> list2 = new ArrayList<Customer>();
        list2 = list1;
        list2.add(a);
        System.out.println("#2: " + list1.toString()); 
        System.out.println("#3: " + list2.toString()); 
    }

}
2
  • 3
    you can not trigger garbage collection. you can only kindly ask the garbe colletion to run (System.gc()). Commented Oct 28, 2019 at 8:45
  • @ Philipp Sander I asked and found there are no..... Commented Oct 28, 2019 at 8:54

2 Answers 2

3

There is no line that causes Garbage Collection, because garbage collection is not something that you as a programmer can trigger. However, after list2 = list1; the ArrayList reference created by ArrayList<Customer> list2 = new ArrayList<Customer>(); is eligible for garbage collection.

Sign up to request clarification or add additional context in comments.

6 Comments

I understand and agree to your point! However, the lecturer asked us to tell her which is the line cause garbage collection.
Maybe debugging the lecturer might give an explanation
I'm afraid this is the only answer in Java, perhaps follow-up with her. It might also be worth asking your lecturer why they chose to use ArrayList<Customer> list1 = new ArrayList<Customer>(); instead of using the diamond operator ArrayList<Customer> list1 = new ArrayList<>(); (Java 7 came out in 2011).
ArrayList<Customer> list2 = new ArrayList<Customer>(); As mentioned by Elliott, line above creates a new object which becomes eligible for garbage collection as soon as you do list2=list1
@another-dave That is true, but this code will almost certainly complete and terminate before garbage collection runs.
|
0

so finally the professor said the answer is list2 = list1; which cause the garbage collection, whereas line 7 is redundant

1 Comment

You might want to ask your professor to explain the purpose of Runtime.gc(); and also what the word "suggests" means in Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. The reality is that such a short program with no obvious memory constraints is highly unlikely to trigger garbage collection during its' runtime. Computers with 32mb of RAM ran garbage collection a lot, you have more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.