• Post Reply Bookmark Topic Watch Topic
  • New Topic

weird java memory behavior  RSS feed

 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output of the above code is:
initialMem = 16192496
finalMem = 16008800
used memory = 183696

How can an Object need 183.696 bytes ?! What is the explanation for this ?

If you like this question, send some cows my way
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This piqued my interest. I went ahead and ran the code you offered and got the following:
initialMem = 251934400
finalMem = 250613152
used memory = 1321248

Well, that's odd - I used more memory than you did!

Curious, I wrote my own little version:

And my output was:
Free Memory: 249539832
Memory with Object: 249539832
Difference: 0

So, perhaps it has to do with the fact that yours has a separate, static method?
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:
So, perhaps it has to do with the fact that yours has a separate, static method?


That is highly unlikely, you do declare an Object, why does it not use any memory ? I doubt the compiler realizes your object will never be used.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucian Whiteman wrote:
Tim Harris wrote:
So, perhaps it has to do with the fact that yours has a separate, static method?


That is highly unlikely, you do declare an Object, why does it not use any memory ? I doubt the compiler realizes your object will never be used.


Comment out the line that calls the garbage inspector and see what happens - when I added it in, I got different results.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if the JVM itself consumes and releases memory, which would make it difficult to precisely measure how much memory an object consumes in this manner.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oooh, a fellow Tim!

I found that the garbage collector is what seems to be taking up so much memory. A couple Google searches confirmed that the garbage collector is bulky, and so I think that's why the memory is getting consumed.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some misleading variable names. For example you have one called initialMemory. But there is nothing initial about it. That is the memory used after the JVM has started and after you have tried to run the garbage collector.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!