Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Garbage Collection  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What objects would be eligible for garbage collection?
 
Sheriff
Posts: 4568
286
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the purposes of copyright and correct attribution, please state where this example has come from.

What do you think the answer is? Are any objects created at all?
 
Marshal
Posts: 61749
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Until Java 8:

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: 61749
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please don';t add points irrelevant to the original question.
 
Anu Gaur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Cooke
Sheriff
Posts: 4568
286
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my understanding, that code will not compile.
 
Anu Gaur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile errors removed :
 
Campbell Ritchie
Marshal
Posts: 61749
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anu Gaur wrote:. . . internal papers of my organization.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 61749
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is that? At the end of line 17?
 
Anu Gaur
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, at line 17, a is pointed to null.
 
Campbell Ritchie
Marshal
Posts: 61749
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!