This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Available Ram...

 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic