0

I need to run a script that contains a call to java. I cannot change that script (as it must be run on a variety of machines), but I can change the environment around it.

I can run a wrapping script around it, if that helps.

My underlying problem is that the java process exits with:

Could not allocate metaspace: 1073741824 bytes

The virtual memory limit is:

$ ulimit -v
2097152

I might also need it to work for javac if thats different.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72

2 Answers2

1

You can reduce the amount of memory java uses by using -Xmx, but that needs to be called on the commandline that calls java. Luckily there are aliases. This is not the same as changing the environment variables directly, so it might not solve your problem ultimately.

So, you write a wrapper script:

#!/bin/bash
alias java="java -Xmx120M"
alias javac="javac -J-Xmx120M"
shopt -s expand_aliases
source /path/to/the/original/script.sh

That way all calls to java someoption Someclass will look like java -Xmx120M someoption Someclass.

Of course this only works properly if there is no -Xmx already present in the script that calls the problematic java code. And it only works if the called script doesn't call other scripts after it.

How does it work: alias replaces one command with another. shopt -s expand_aliases turns the aliasing feature on (but its resetted when a new script starts) source ...somescript.sh loads the other script inside this script as if it where one. This prevents the drawback of shopt mentioned above. But only one layer deep.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
1

There is an inbuild method of supplying java parameters to programs where you can't specify the building of the JVM (such as your case, but also if the java command originates inside a binary) and its called JAVA_TOOL_OPTIONS.

In your case you would set:

JAVA_TOOL_OPTIONS="-Xmx120M"

to your environment.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72