Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to (re)launch a Java application with hardware-dependent VM parameters?

EDIT I don't want to use Java Web Start

I've got a Java application that I'd like to run with different VM parameters depending on the amount of memory the system it is launched on has.

For example if the machine has 1 GB of memory or less I'd like to pass "-Xmx200m" and "-Xmx400m" if it has 2 GB and "-Xmx800m" if it has 8 GB (these are just examples).

Is there a portable way to do this?

I've tried having a first tiny Java app (hence portable) that determines the amount of memory available and then launches a new Java app but I don't think this is very clean.

As of now I've written Bash shell scripts that invoke the Java app with the correct parameters depending on the config but it only works on Linux on OS X.

What is the correct way to solve this?

Would application packager package ;) help ?

like image 829
LowLevelAbstraction Avatar asked Mar 03 '10 10:03

LowLevelAbstraction


3 Answers

Are you sure you want to do this? In general, Java application need a certain amount of memory, not more, not less.

If you have a machine with little memory and you specify a small number at -Xmx you risk to run out of memory.

If you have a machine with lots of memory and you specify a large number the application won't use all the memory but only the amount required by the objects in use.

So, in general, you don't lose memory by specifying large numbers at -Xmx.

(Exceptions are server applications which have a large amount of object 'throughput' and tend to waste memory.)

like image 152
Wolfgang Avatar answered Nov 18 '22 13:11

Wolfgang


What's your gripe with your current approach, exactly? It's completely portable (unlike various scripting solutions... why is a script "cleaner" than 100%-portable bytecode?), and it'll adapt perfectly if the hardware changes after installation (so it's better than capturing the hardware details as part of your installation script).

If you can explain the actual issues it may be easier to come up with a solution to those specific problems.

Here's an alternative that you might like better, actually: copy what Photoshop, GIMP, etc. do and make "resource usage" or "performance" part of the preferences for your app. You'll have to prompt them restart your app when/if they change this setting (to relaunch your app using the modified start procedure, since you can't change the memory limits for a running app... where you write the new flags is deployment-method dependent), but that's not such a disaster for a setting they'll likely change exactly once.

You could also check their hardware on first run, and if your default installed setting is way off from their setup, prompt them to change the setting then.

like image 27
Rob Whelan Avatar answered Nov 18 '22 11:11

Rob Whelan


Since http://ant.apache.org :

  1. is a java program
  2. has built-in forking/launch mechanism
  3. has scripting basics
  4. is embeddable within another program
  5. has command line parsing and other basic stuff that deals with environment/config issues.

I suggest that you

  1. Create a build.xml (or create the targets/tasks programatically)
  2. and have the your initial program be ant running the build.xml script
like image 34
Pat Avatar answered Nov 18 '22 11:11

Pat