• 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

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

Hi All,

I have written this following code to check the garbage collection and expecting that garbage collection will not occur since I'm maintaining the reference of objects.


But when watching the memory graph in JConsole it was sawtooth like graph indicating garbage collection.Will there any garbage collection for this code ?
 
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, the occurrence of GC has nothing to do with your code/app.
 
Arka Sharma
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes.So that means that objects in heap area will be there until program runs.No garbage collection will occur right ? But when I'm monitoring the memory graph in JConsole it is showing sawtooth like pattern.

Another question is will this program end up with OutOfMemoryError ?
 
Sumit Bisht
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, your objects would be there.
However, the GC would occur as there are other objects too on the JVM heap (eden/survivor/etc) and the Garbage Collector collects and compacts them (while moving the objects internally, for optimization)
Yes, the program would eventually result in an error as you mentioned.
 
Arka Sharma
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So that means that heap memory usage for this process will be monotonously increasing.But it is not like that.Because JConsole is showing a sawtooth graph indicating continuous allocation and deallocation of objects from heap.
 
Sumit Bisht
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are missing the picture, Inside the jvm there are other objects too which have to be managed.
If you remove the sleep() call, you'll notice the memory issue faster.
Also, what you are watching is the jvm heap, not the heap of your 'process'.
For that, you'll need some advanced profiler like Netbeans/VisualVM or Oracle Mission control.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If new objects are being allocated, JVM will eventually run GC even if there are no objects eligible for GC (only way to determine if there are such objects is to run the garbage collector).

Also your code calls methods which may create extra objects and then throw them away.

1. When the ArrayList exceeds its capacity, it has to create new bigger array and copy items from old to new array. The old array then becomes eligible for GC.
2. Println may create temporary objects.


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

@Vlado Zajac

I got what you want to say.But still since objects are being allocated and their references are being stored it should increase the heap usage of that process.How to track that ?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code is adding an Integer object to the list every 500 ms (so, every second, it adds 2 Integer objects to the list). Since an Integer object is quite small (only a few tens of bytes), it will take a really long time before you're going to notice that it's using more and more memory.

Try making the sleep period a lot shorter (to a few ms), or removing it alltogether, and you'll see the memory usage grow much faster.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic