• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Available Ram...

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic