• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Garbage collection

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 1 class GCTest
2 {
3 public static void main(String[] args)
4 {
5 Double x = new Double(0);
6 for(int i=0;i<5;i++)
7 for(int j=0;j<3;j++)
8 x = new Double(i+j);
9 int a = x.intValue();
10 System.out.println(a);
11 }
12 }

How many object will be eligible for garbage collection after execution of line 9.

option 1)8
2)14
3)15
4)16

please give me the answer and explain the same
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the answer is 15: First x is assigned new Double(0) ... untill now everything is clear , than follows two "for" nested loops and every iteration creates a new object and assigns it to x, thus with each iteration a new object is available for GC (except for the one created in the last iteration).
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

yes should be 15. The first is the Double(0) from line 5 that gets eligible as soon as x is dereferenced.
The loops make 5*3=15 objects, the last one remains = 14


14 + 1 is about 15.

But I cheated a bit. I wrote some code. See below.

Yours,
Bu.
 
brevity is the soul of wit - shakepeare. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic