• Post Reply Bookmark Topic Watch Topic
  • New Topic

I tried a lot but unable to find the reason for this .....  RSS feed

 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the following code and see the output, I tried to figure out and searched on net to get the reason for this behavior but I did not get any right clue for that ....

Can any one give the right clue, and let me try more based on that clue,




OUTPUT:
--------------

Before 174032

After 107704
[ September 20, 2006: Message edited by: Srinivas Kalvala ]
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you change the code so that it calculates and prints out the amount of memory used inside the loop after the gc() call, do the results make sense?
[ September 20, 2006: Message edited by: Ryan McGuire ]
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I did not get you Ryan.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Not an Advanced question)

What's this code supposed to show?

I suppose totalMemory() minus freeMemory() will give approximately the amount of memory in use by objects on the heap. When a full GC runs, all objects on the heap that are no longer referenced get freed. That will mean freeMemory() increases, so the difference reduces, like you saw.

Note first that calling gc() doesn't guarantee to do a full GC, or indeed anything, even if you call it hundreds of times (what's the idea of that anyway?). Second, note that, depending on JVM settings, the totalMemory() may change, as the JVM adapts the heap size to the program's requirements.
[ September 21, 2006: Message edited by: Peter Chase ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. And never forget that a JVM, even one running a very simple user program like this, is a multithreaded process. Other stuff is going on in the background, and that other stuff may very well be allocating (and disposing) memory.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!