Please also copy the question exactly. You must say when you are counting objects eligible for GC. Also copy the code exactly; I don't know how many of the compiler errors were in the original and how many are because you introduced copying errors.

Garbage collection has more profound impact on the application in contrary to what most engineers think. In order to optimize memory and garbage collection settings and to troubleshoot memory-related problems, one has to analyze Garbage Collection logs.
Enabling GC logs
GC Logging can be enabled by passing below-mentioned system properties during application startup

Below is the system property that is supported by all version of Java until JDK 8.

From Java 9:

Below is the system property that is supported by all version of Java starting from JDK 9.

Campbell Ritchie

Marshal

Posts: 62881

203

posted 3 months ago

Welcome to the Ranch

Please don';t add points irrelevant to the original question.

Anu Gaur

Greenhorn

Posts: 11

posted 3 months ago

I got this question in one of internal papers of my organization. I dont know exact reference.

Also, as per my understanding, all objects created ll be valid for garbage collection at the end of program as main reference is made to point null and thus all objects created ll be lying on heap without any external reference.

I hope you are allowed to quote it, though I can see no harm in the code you quoted myself.

. . . all objects created ll be valid for garbage collection at the end of program as main reference is made to point null . . .

That is correct, assuming the code will run at all. But for an exam, they will want to know how many objects are eligible for GC.

Anu Gaur

Greenhorn

Posts: 11

posted 3 months ago

Two objects should be created one of SmallClass and one of BigClass. Two references a and bigClass should be pointing to BigClass object and one reference smallClass should be pointing to SmallClass object. As soon as a is pointed to null, all other references are pointing to each-other somehow and not touched by external program thus valid for garbage collection.

Campbell Ritchie

Marshal

Posts: 62881

203

posted 3 months ago

Where is that? At the end of line 17?

Anu Gaur

Greenhorn

Posts: 11

posted 3 months ago

yes, at line 17, a is pointed to null.

Campbell Ritchie

Marshal

Posts: 62881

203

posted 3 months ago

I think you are right about two objects being eligible for GC after line 17, but how may objects can you see not eligible for GC?

Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this: