This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes java heap memory check Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java heap memory check" Watch "java heap memory check" New topic
Author

java heap memory check

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 172
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
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

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

Joined: Jun 28, 2009
Posts: 172
Hi Paul,



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




[HENRY: Fixed code tags]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

Jacob,

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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java heap memory check
 
Similar Threads
puzzle of OutofMemory.
Runtime methods
Memory Java heap Issue:Need Help
How to check how much memory is used by a program??
Total memory won't reduce after freeing memory allocated for String?