1

The answer provided to "increase the java heap size permanently?" doesn't actual answer the question. At least in the sense I was expecting. I would have added this detail in a comment on the same page, but I have not earn that privilege, but I digress. The answer in that question states to export the following like so: export _JAVA_OPTIONS="-Xmx4g". Which works, but not after rebooting the server.

This is an exported variable, and as the another member states further below, an alias can be added to the .bashrc file for that user. This means that setting is invoked only when that user's environment is invoked. Right? What I would like to know is how to get this MaxHeapSize setting noted here:

$ java -XX:+PrintFlagsFinal -version | grep -iE 'MaxHeapSize'
Picked up _JAVA_OPTIONS: -Xmx2g
    uintx MaxHeapSize                              := 2147483648      {product}
java version "1.6.0_37"
OpenJDK Runtime Environment (IcedTea6 1.13.9) (rhel-1.13.9.4.el6_7-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

Permanently set even after the server is rebooted. I have a bash script that executes a jar file to handle images, and this is all run by the Apache user. Which doesn't have a shell environment. Adding the mentioned export string to the script environment doesn't render the desired uintx MaxHeapSize value. If I simply export the desired MaxHeapSize, I see the value change, but there has to be a way to set this "permanently" so that I am not depending on a environment variable. That is the answer I am looking for, where the answer provided in the other post seems to be a work around. How can one set/change the default settings of the Java min and max heap sizes to permanently retain after reboot and not dependent on a exported environment variable. I have servers with different default values so it appears to be possible.

REVISED After reading the first two comments, this is the script that executes the jar:

PATH=$PATH:$HOME/bin:/usr/java/jdk1.5.0_05/bin
CLASSPATH=.:/home/hg/bin/imageutils.jar:
export CLASSPATH
export PATH
java com.hg.image.Thumbnail $1 $2 $3 $4 $5

I tried adding the following to the top of the script: export _JAVA_OPTIONS="-Xmx4g"

But that didn't change the vaue of the MaxHeap size when I ran:

$ java -XX:+PrintFlagsFinal -version | grep -iE 'MaxHeapSize'

trincot
  • 317,000
  • 35
  • 244
  • 286
H-man
  • 163
  • 1
  • 2
  • 12
  • You have to add `export _JAVA_OPTIONS="-Xmx4g"` to the script or shell in which the server is started, e.g. to the running user's `.bashrc`. Calling `export` is no different to defining a local variable in a program: you have to set it again when you re-run that program – Andy Turner Jul 11 '16 at 15:27
  • 1
    *"I have a bash script that executes a jar file"* So add the option to that script! In that script you execute `java -jar myjar.jar`, so just change that to `java -Xmx4g -jar myjar.jar`, and that script will *always* (permanently) use 4G for that Java program. – Andreas Jul 11 '16 at 15:29
  • Added my script details to the question. So based on both of the responses, there is no way to change the default value for the heap sizes other than exporting the _JAVA_OPTIONS= ? Also, as I mention in my revised portion of my question, I added the mentioned export to the script and it didn't work. Values are still the same after the script is executed. So its either working and I can not witness it, or its not working. – H-man Jul 11 '16 at 15:46

1 Answers1

1

Which works, but not after rebooting the server.

It needs to be set every time you start a new environment if you want to happen when you restart. This is not a feature of Java but rather how environment variables work in the shell.

I have a bash script that executes a jar file to handle images, and this is all run by the Apache user.

I would add it to bash script so it is set each time. This is what the script is for.

How can one set/change the default settings of the Java min and max heap sizes to permanently retain after reboot and not dependent on a exported environment variable.

The default maximum is 1/4 of main memory which is why it changes based on the memory size.

You can compile your own version of the OpenJDK (as that is what you are using) and in there you can change the default.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks. This part of your answer really help clarify things: "The default maximum is 1/4 of main memory which is why it changes based on the memory size." – H-man Jul 11 '16 at 15:50
  • I guess my final follow up question,after adding the following to the top of my bash script `export _JAVA_OPTIONS="-Xmx4g"` which is executed by Apache user when the application is used, should I be able to witness the heap increase when running the `$ java -XX:+PrintFlagsFinal -version | grep -iE 'MaxHeapSize'` command as root? currently I am not. – H-man Jul 11 '16 at 16:10
  • 1
    @H-man No, because you're not executing the script, so you won't see the effect. – Andreas Jul 11 '16 at 16:16
  • @H-man would you what a user to be able to to make a change which would alter what root does. e.g. `export _JAVA_OPTIONS="; runCommandToMakeMeRoot;"` ? – Peter Lawrey Jul 12 '16 at 07:36