Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

about GC

 
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please tell some thumb rules so that i can tackle questions on GC . I,am doing fine with true/false type of questions , but then it comes to code i get into trouble ( in case of GC) .
need your help
regrads
raghav...
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find that it usually helps to draw a picture. When you see the word "new", draw a new object. Whenever an object is assigned to a reference, draw a line from that variable to the object that it references.
As soon as there are no more lines pointing to an object (from an active part of the program), that object is eligible for garbage collection. When you have no lines left, there are no references left to that object.
Although this is not what it was originally created for, you can see what I mean if you look here.
I hope that helps,
Corey
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The system internally keeps a count of the number of references that are currently active for a given object. When a new reference is added, that reference counter is incremented by 1. Conversely when a reference is removed it's decremented. Once that internal reference count reaches 0, an object is eligible for garbage collection.
Corey's flash animation is a great tool, and his suggestion of "drawing lines" for each reference will help you diagram out what's going on.
A couple of more bits of information related to Garbage Collection (some on the test, some just nice to know).
- Garbage Collection algorithms are largely left up to the VM implementer. After an object is marked available for collection there is no definite way to tell when it is actually going to be collected.
- According to the spec, calling System.gc() only SUGGESTS that system expend effort performing garbage collection. It does NOT guarantee it. Again, this is an implementation detail left up to the VM creator.
- Garbage collection runs as a lower priority thread than your applications do. If you have a very busy system, the GC may not run for a long time. I've actually seen this on some heavily loaded servlet based systems where data piles up for hours before GC finally gets a chance to kick in.
- The garbage collector will not collect an object until it's finalization is completed. It's better to clean early than rely on finalize for cleanup. Otherwise it may take more than one pass of the GC thread to reclaim the memory that was allocated.
For more insights into GC and finalization, check out the discussion in the JLS
Macon
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still confused

Originally posted by Corey McGlone:
I find that it usually helps to draw a picture. When you see the word "new", draw a new object. Whenever an object is assigned to a reference, draw a line from that variable to the object that it references.
As soon as there are no more lines pointing to an object (from an active part of the program), that object is eligible for garbage collection. When you have no lines left, there are no references left to that object.
Although this is not what it was originally created for, you can see what I mean if you look here.
I hope that helps,
Corey

 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please help me .....!!! . i,am still confused about GC .
thanks a ton in advance
regards
raghav mathur
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you are clear about basic GC stuff and related issues - you should consider reading about different references - like Phantom, Soft, Weak etc. All from java.lang.ref.
Especially if you preparing for a interview or something like that, let this be the secret weapon to smash your compition!
Ashok.
[ March 15, 2002: Message edited by: J Ash ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great article:
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please provide me with some good thumb rules so that i can tackle the questions on GC in the exam .

Originally posted by Valentin Crettaz:
Great article:
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Corey or anyone
What happens when objects are created without any reference such as a String doing a concatenation operation or an object created as new object() there would be no reference. Is it that in these cases as soon as the operation is over it is ready for GC
Please advice
Thanks in advance
Kareem
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic