aspose file tools*
The moose likes Java in General and the fly likes Available Ram... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Available Ram..." Watch "Available Ram..." New topic
Author

Available Ram...

Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
I think I know the answer to this but I'm going to ask it anyway.

Is there any possible way to find out how much RAM is installed on a machine from java? I realize this is fairly system/OS specific and is most likely impossible to get using pure java but if there is a way (or an easy way to cheat for the major OS's Windows, linux, bsd). I would greatly appreciate hearing how.

thanks,
-Tad
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
I think you can't do this using purely Java code bcz the reasons you said.
You could try to call a specific-system library through JNI (a .so in Linux or a .dll in Windows) to get this kind of information.


SCJP, SCWCD
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

There is no method in the standard library to find out how much memory the machine has that your program is running on. However, class java.lang.Runtime does contain some useful methods: for example maxMemory(), which will return the maximum amount of memory that's available to the JVM.

Normally, the JVM will not allocate more memory than that maximum. You can set the maximum amount of memory on the command line with the -Xmx... option, for example:

java -Xmx256m MyClassName

will set the max. amount of memory to 256 MB.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
java -Xmx256m MyClassName


I'm trying to find an easy way during the install of the app to determine what value to to put after the -Xmx with out asking the user.

-Tad
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

What you put in there is the amount of memory the application actually needs. You should determine that number before you distribute the application.

And it is possible to put in -Xmx... a number that is larger than the machine's physical memory (I have done it). That just causes the JVM to use virtual memory, which is not a bad thing. Okay, it's not a good thing, but at least not a very bad thing.
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
What you put in there is the amount of memory the application actually needs. You should determine that number before you distribute the application.


Well how much the application needs changes based on the size of input.

And the last time I put a number there that was equivalent to the physical ram on my machine it crashed either the application or the machine (I can't recall, but it did bad things).

-Tad
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Well how much the application needs changes based on the size of input.

Sure, there's nothing unusual about that. But I don't see how it helps to know how much physical memory is available. No matter what the answer is, you don't have infinite memory, so if you have an application that can potentially use infinite memory (again, nothing unusual about that) then you need to write it so that it deals with that problem somehow. And then pick a non-infinite number that you can make it work within.

It's pretty standard for packaged software to say "Requires X MB memory to run". Seems to me that Java programs should be entitled to say the same thing.
And the last time I put a number there that was equivalent to the physical ram on my machine it crashed either the application or the machine

I have 256 MB of physical memory on my home system and I run a Java application with -Xmx400M in the command line, with no problems.
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264

I have 256 MB of physical memory on my home system and I run a Java application with -Xmx400M in the command line, with no problems.


Well then why bother with the flag at all? Why not just put -Xmxinfinite? and let the machine top out whenever?

I think it'd still be helpful to know how much physical ram is available to in a friendly way set that max at a percentage of whats available.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Well - the amount of needed memory depends on input, not on the available memory, and users might change the available memory by buying RAM or resizing swap, so I would go for a reasonable default value, and a well documentation of how and when to change that value, especially in an catch(OutOfMemoryException oome)-block.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Available Ram...