0

I got 2 classes.

  1. ToyCar
  2. ToyShop

ToyShop has a toyCar field which accepts a ToyCar object.

public static void main(String[] args) {
  ToyCar[] cars = new ToyCar[3];
  cars[0] = new ToyCar();
  cars[1] = new ToyCar();
  cars[2] = new ToyCar();
  ToyShop company = new ToyShop();
  company.setToyCar(cars[2]);
  cars[0] = null;
  cars[1] = null;
  cars = null;
  print(company.getToyCar())
}

If I run the program, will the cars array be garbage collected, or just cars[0] and cars[1] is collected??

If the array is garbage collected, can I print out toyCar in company??

If my question does not make sense, please point out.

4
  • 4
    it will be eligible for garbage collection, but since you can't 100% control when the GC runs .. Commented Jan 25, 2019 at 9:03
  • If you read up on garbage collection you'll note that any data that isn't reachable by a live thread is eligible for garbage collection (i.e. it could be collected but whether it is depends on the collector). That being said, after your application ended (at the end of the main method) the array referenced by cars could probably be collected (if there are no other references to it that we don't see) but since the application would stop at that point anyways the question is a little moot. Commented Jan 25, 2019 at 9:04
  • At the point of print(company.getToyCar()), is the array gc'ed? Can individual items in array being gc'ed instead of the whole? Commented Jan 25, 2019 at 9:07
  • What question are you actually trying to answer? GC runs when the JVM says it'll run. Are you asking if cars[2] will still be available? What happened when you tried it? What do you think should happen? Would it be reasonable to GC something that still has a reference? Commented Jan 25, 2019 at 9:09

2 Answers 2

2

Almost certainly, the program will complete before the GC runs. So ... technically ... nothing is garbage collected.

Similarly, when the main method finishes, all of the objects that it created will be unreachable and eligible to be garbage collected. (Even if the program didn't terminate.)

However, when you get to the print statement, the company object will be reachable and its toy field will be reachable, so the value of that field will be reachable ... and the ToyCar object it refers to won't have been garbage collected, whether or not the GC has run by then.

The general rule is that if your application can reach an object, it won't be garbage collected. Basically, don't worry about it. The object will still be there if your code can use it.

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

Comments

1

Here these three lines of code construct three different new objects of ToyCar

  cars[0] = new ToyCar();
  cars[1] = new ToyCar();
  cars[2] = new ToyCar();

After execution of these two lines of code

cars[0] = null;
cars[1] = null;

first two car object will be eligible for garbage collection. because there is no external reference to that object exists in JVM.

Now Come to this line

ToyShop company = new ToyShop();
company.setToyCar(cars[2]);  //now car object at 2ond Index have external ref.

Here third Object reference is assigned to a reference Variable toyCar present as member variable in company.

So, After execution of line cars = null; there is still one external reference present in JVM.

So, Only 2 Object will eligible for garbage collection after execution of cars = null;.

Comments

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.