• 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

Garbage Collection - outOfMemory

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output:
bye
Exception in thread "main" java.lang.OutOfMemoryError


Why this program is printing bye followed by OutOfMemoryError
some one please explain this code.
If I hide line 5 and the while loop then there is no runtime error.
please explain this too.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vishnu,

The outofmemory error is due to the fact that you are adding new strings to the arraylist which would need to grow as items are added to it. Th arraylist is backed up by an array which cannot grow beyond a point as the strings created are all part of the list and they cannot be garbage collected. So the outofmemory error is thrown.

When you say commented out line 5, I assume that you are just creating new strings but not adding them to the arryalist. In this case, the strings are free to be garbage collected since we are not storing them any where. So the GC makes sure that JVM doesnot run out of memory.

And regarding that 'bye' getting printed is because when GC runs, it reclaims unreachable objects. Since you set myType to null, the object is GCed and before that its finalize method is called.

Hope this helps.
[ June 12, 2005: Message edited by: Reghu Ram T ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic