Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java heap memory check

 
Jacob Sonia
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • 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]
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,



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




[HENRY: Fixed code tags]
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob,

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

Henry
 
Paul Clapham
Sheriff
Pie
Posts: 20769
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic