This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

gc after for loop

 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen in a couple of mocks a question along these lines...
for (int i = 10; i >=0; i++)
{
System.out.println(i);
}
//
and then they ask you at // how many items are available for garbage collection. I always get it wrong because I say 11 (0 - 10), but the answer is always 10 and it says something about there being one instance still held somewhere. Does anybody know what I'm talking about, because I've NEVER understood that one. I think it's in the RHE mocks. ANY help at all would be appreciated.
Matt
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt,
Can you Pls explain the question indetail....Primitive data types are not under instance group...In your code you are incrementing the value of i but not creating any instances on every iteration.
Can you pls explain the question in detail.
Regards
Prasad
------------------
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt,
Could you just post an exact reference to the qustion or the question itself, it would then be much easier to solve your doubts.
Shubhangi
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There exists some very detailed discussion on this topic in earlier posts. do find them before you reinvent the wheel
 
Shubhangi A. Patkar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mohit joshi:
There exists some very detailed discussion on this topic in earlier posts. do find them before you reinvent the wheel


Mohit
could you please point us to the link you mean? The forum is so huge, it is giving nearly 200+ results for searchon term "garbage collection".
TIA,
Shubhangi.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasad,
as u know System.out variable is a PrintStream object.
In the PrintStream's println(int) the following methods is called,
String.valueOf(int);
valueOf(int) method in the String class calls the Integer.toString(int), which creates a new String object.

So, passing the primitives to the System.out.println() method creates a String object.
Passing an Object to the System.out.println() method calls the objects
toString() method if the object reference is not null, if the object reference is null, a new String "null" is created and written to the stream.
---------------------
Hi Matt,
in the following code it looks like all the 11 objects will be
garbaged collected !.
When the output stream objects get a String object,
it will take the characters out from the String and outputs to the underlying sink ( it may use char[], or byte[] for buffering and flushes the buffer).
So y the PrintStream should hold the reference to the last Object in the for loop. rite ??
So i guess ur rite, 11 objects will be garbage collected

Correct me if am wrong

[This message has been edited by Jon Aryan (edited November 14, 2000).]
 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose my question is, does the PrintStream hold the reference after the for loop is complete? I would think it would not, because it then goes out of scope, but the answer to the mock suggests that it does. The mock answer is 10 even though I feel that it should be 11. Strange, perhaps it's just errata.
Matt
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this


and this


[This message has been edited by mohit joshi (edited November 14, 2000).]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read most of the referenced thread from this and there are a number of interest points. However, I agree that it would be 10. Although "i" goes from 0-10 the system only prints 1-10 (the loop is exited before printing 0). Therefore there are only 10 strings created, there could never be more than 10 objects to garbage collect.
Lisa
 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, actually the loop iterator ought to be i--...if it WAS...
0 WOULD be printed...not sure i see where you are coming from Lisa...sorry. Mohit, thank you very much...I followed those links...to me it seemed like there is absolutely no consensus.
Matt
 
Die Fledermaus does not fear such a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic