• 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

java heap memory check

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

I am trying this code for checking java heap memory, but somehow the stats doesn't look right




free memory: 3,117
allocated memory: 4,096
max memory: 1,048,576
total free memory: 1,047,597

total 4194304used 1004064
free memory: 3,115
allocated memory: 4,096
max memory: 1,048,576
total free memory: 1,047,595

total1 4194304used1 1004192


Looking at the first stats, it looks like a long takes 2 kb of memory. And the second stats, which is in bytes say that it is taking around 128 bytes of memory. Not sure if it is correct.

Thanks,


[HENRY: Fixed code tags]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are so many things wrong with that, I don't even know where to start.

Okay. Let's try one thing: you assign values to three long variables, then you do some String concatenations, then you create a StringBuilder object. And you assume the difference between the free memory before this and the free memory after that is... the amount of memory used by a long variable?

Let's try another thing: the space for local variables is allocated at the start of the method. So even if you didn't create all those other objects, you still wouldn't be measuring the space required for a long variable.

And rounding your numbers down to the next lower multiple of 1024 isn't going to help you tell if a long variable requires 8 or 16 bytes.
 
Jacob Sonia
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,



Thanks for the answer. I tried this..is this the same...




[HENRY: Fixed code tags]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jacob,

Please fix your code tags, it's not very readable the way you have it.

Henry
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like that code is trying to calculate how much heap memory is used. Am I right? But local variables use stack memory, not heap memory.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic