tags:

views:

824

answers:

2

I use the following lines to get the memory usage with Java :

Free_Memory=Run_Time.freeMemory()/1048576;           // 1024 x 1024 = 1K x 1K = 1Meg
Total_Memory=Run_Time.totalMemory()/1048576;         // 992 Total on a 4 GB PC

The Free_Memory I got was : 900, but it is way off, when Free_Memory goes down to around 600, my program ran out of memory and generated heap overflow message.

So I looked at the => Windows Task Manager : Performance : Physical Memory : Free, it's down to 1, 2 or 0, which is a more accurate reflection of my memory situation, and according to it, my Total Memory is : 4089, which is correct, while Java's Total_Memory=992 is incorrect.

So, my question now is : In my Java program how to get the memory usage numbers reflected in the Windows Task Manager : Performance : Physical Memory ? I need to depend on those numbers.

+2  A: 

The JVM doesn't allow Java to consume all available system memory. Instead the JVM grabs a fixed chunk and allocates all of your objects within that chunk. If this area fills up, you're out of memory! There are commandline options to alternate the max/initial memory usage of the JVM.

The more important issue is that you should not be relying on tracking free/max memory. What are you doing that relies on tracking memory?

UPDATE:

Try 64bit if you need more memory than 1.5GB

If you're trying to track memory running out then consider figuring our WHY your program does this and if it can be prevented through different algorithms or better management of objects. When the memory reaches zero what do you expect to do? Popup a dialog and tell the user they're screwed and exit the program? I can understand a grateful shutdown but warning the user to run with a large -Xmx is not going to cut it.

basszero
I want that info so I can see how close my program is running toward 0 free space, and modify it accordingly. I have 4 GB on my PC, and yet if I allocate more than -Xmx1570M, it will not allow, is that because I'm running 32-bit version of Java ? Would it allow more space if I run 64-bit version of Java ? I do have 64-bit of Vista on my system.
Frank
Yes, 32-bit java won't use more than 1.5gb. Try 64-bit java.
basszero
It's because I have 100 days worth of data need to be analyzed, but since I don't have enough memory, I can only consume 10 days of it, so I need to know how low I can go and if I can add another additional day's data to it, if so that's a 10% increase, quite a lot.What's the max memory limit on 64-bit Java ?
Frank
+1  A: 

If you want detailed Windows stats, you can use WMI and a .vbs script, executed via cscript.exe.

This link details a script that pulls more detailed memory stats than you could possibly want.

Execute this via the usual Process/Runtime combination, and simply read back what figures you require. These are system level stats, and not for the VM (although WMI can pull back per-process stats as well).

Brian Agnew
Sounds nice, but it'll be more helpful if you can post a few sample lines of how to do it. I'm not familiar with any scripts. Also, is cscript.exe part of Vista system files ?
Frank
I've used this in the past but I'm afraid I don't have examples to hand. So I would have a Google around. Sorry - if I had examples to hand I'd be happy to post them up. I've used this mechanism in the past and it works well
Brian Agnew
cscript.exe was freely available, but I think I had to package it up in my deployable .jar since it may be an additional download from Microsoft. It looks like it's available on Vista though - http://www.programchecker.com/file/32638.aspx
Brian Agnew