• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Garbage Collection  RSS feed

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I want to know whether the objects referenced by b and a will be eligible for garbage collection or not after the line a=null executes.

Thanks.
 
Rancher
Posts: 821
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akshay,

I believe the answer is yes, because there are no live references at that point.

Les
 
Akshay Rathore
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain it visually?

I see circular referencing here and are circular referenced objects eligible for garbage collection?
 
Les Morgan
Rancher
Posts: 821
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Live reference a => A, A => B, B => A (same A as references B)

You still have a live reference from the program context

You execute a = null;

Now you have a => null;

and you also have orphaned Objects of type A and B that reference each other, but they are not referenced from a live context, so they are marked for garbage collection. That was quite the problem back in the late 90's and maybe as recent as the early 2000's.
Back in the day that accounted for many a memory leak using Java.

As a practical example: a circularly linked list could never be garbage collected unless you traversed the list and removed all the references.

Akshay Rathore wrote:Can you please explain it visually?

I see circular referencing here and are circular referenced objects eligible for garbage collection?

 
Akshay Rathore
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was not aware that circular referenced objects are eligible for garbage collection.

Thanks for the reply.
 
Les Morgan
Rancher
Posts: 821
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it was back in Java 4 or 5 they fixed it so you have to have a reference from a live scope or they are orphaned objects and collected.

Akshay Rathore wrote:I was not aware that circular referenced objects are eligible for garbage collection.

Thanks for the reply.

 
Marshal
Posts: 24464
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was long before that, I'm sure. They might even have had proper garbage collection (which handled orphaned loops of references) from the very start. I remember switching from Visual Basic to Java and being relieved that I didn't have to deal that issue any more, and I remember that when I was flailing around trying to figure out Java as a raw beginner, I had to deal with the fact that you specified class names differently (in the command line) between Java 1.1 and Java 2.
 
Les Morgan
Rancher
Posts: 821
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back in the day--around 2000--there was quite the problem with "weak references" that would not allow the garbage collector dispose of objects. I remember quite the discussion on the Sun forums about it and it was a real problem. Even Sun's JAI suffered from that for a while. I entered, in a big way, about Java 1.2--java 2.0--and before that Java was more of a curiosity that I could not get any of my employers to venture into, but at Java 2, as it was put by one lecturer: "Java got legs!" The GC was really improved in 3 and 4, and after the implementation of 4 I quit seeing any memory leaks from orphans and GC.

Paul Clapham wrote:It was long before that, I'm sure. They might even have had proper garbage collection (which handled orphaned loops of references) from the very start. I remember switching from Visual Basic to Java and being relieved that I didn't have to deal that issue any more, and I remember that when I was flailing around trying to figure out Java as a raw beginner, I had to deal with the fact that you specified class names differently (in the command line) between Java 1.1 and Java 2.

 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!