-3

I am trying to increase the java heap memory in our production server . Here is the two way by which I am trying .

Way 1

I have tried the following answer from this link. I have set java memory by the following command .

export _JAVA_OPTIONS="-Xmx10g"
java -XshowSettings:vm 

In this way , java heap size is not permanently increased .

Way 2

I have tried this answer. I have tried to increase java heap size by the following command .

java -XX:+PrintFlagsFinal -Xms5240m -Xmx10240m -Xss512k -XX:PermSize=64m -XX:MaxPermSize=128m  -version | grep -iE 'HeapSize|PermSize|ThreadStackSize

In this way , java heap size is not permanently increased .

Conclusion

In the above way , I could not be succeeded to change java heap size permanently . So how can I change java heap size permanently ? Please suggest me a way . Thanks in advance.

Updated :

Here I want to mean the following meaning by 'permanently' word .

Meaning : If I run the following command , then I want to see the heap size same every time I open the shell .

java -XX:+PrintFlagsFinal -version | grep -iE'HeapSize|PermSize|ThreadStackSize'   
Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68

4 Answers4

2

As you're tagging the question with "tomcat", I disagree with the answer you posted. Or at least with the applicability of that answer: When tomcat starts, you want to set certain options. When you configure any java process that ever starts to have those options, you're setting yourself up for disaster:

When you shutdown tomcat, it starts a very short-lived Java process that connects to the running server, signals it to shut down, then terminates. For this operation you don't need any specific memory, rather, this can fail if you don't have 5G of free memory (-Xms5000m) when you want to shut down tomcat.

If you start jconsole in order to inspect the tomcat JMX beans, to see what's going on, you'd need 5G again. And for any other little java tool that will run on your computer and that you don't even know about yet as well. (as you make the setting truly global: Any process that any user starts might impose those humongous resource requirements.

Now, what to do instead?

For tomcat, in a Linux environment (you tag "redhat"), create a file in tomcat's bin/setenv.sh and set the variable CATALINA_OPTS, e.g.

CATALINA_OPTS="$CATALINA_OPTS -Xms5000m -Xmx5000m"

If Tomcat is running as daemon, it might be better to have this setting in other places.

To be complete, even though you're not tagging Windows: For Windows, do the equivalent in bin/setenv.bat. Those files are not part of the tomcat distribution, e.g. they won't be overwritten upon updates. But if they're there, they'll be taken into account. For Windows services, use the service configuration for the appropriate settings

Another recommendation: For a production server, you want to set -Xms to the same value as -Xmx. Reason: If the maximum memory is not available at startup, you want to figure that out immediately, not sunday night at 3am, when the JVM decides that it'd like to have a larger chunk of your memory.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Can I start the tomcat server by ./startup.sh after setting the parameter in bin/setenv.sh ? Or I have to start tomcat server by ./catalina.sh ? – Christopher Marlowe Sep 06 '18 at 08:29
  • yes, absolutely. `startup.sh` calls `catalina.sh` in the end, and `catalina.sh` embeds `setenv.sh` (if it exists). So both, `./startup.sh` and `./catalina.sh run` work. – Olaf Kock Sep 06 '18 at 10:09
  • A second valid reason for setting -Xms and -Xmx to the same value in a stable (production) environment is efficiency/performance; if you are eventually going to expand up to -Xmx you may as well grab that memory immediately, rather than getting it dynamically as the need arises. – skomisa Sep 18 '18 at 03:41
0

Just adjust memory arguments in java runnable instance you need.Every Java instance have itself jvm space.

tiny
  • 21
  • 3
0

On Rhel7 or CentOS7, you should change /usr/lib/systemd/system/tomcat.service:

Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
Idan Str
  • 614
  • 1
  • 11
  • 33
Jwang
  • 1
-1

After 4 hour long query , I have found the following solution .To modify the system-wide profile go to /etc/profile . Alternatively you can run the following command .

 1. vi /etc/profile
 2. export  _JAVA_OPTIONS="-Xms5000m -Xmx12000m"

To check the java heap memory size run the following command :

java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'

This command outputs the following in my pc .

Picked up _JAVA_OPTIONS: -Xms5000m -Xmx12000m
     intx CompilerThreadStackSize                   = 0                                   {pd product}
    uintx ErgoHeapSizeLimit                         = 0                                   {product}
    uintx HeapSizePerGCThread                       = 87241520                            {product}
    uintx InitialHeapSize                          := 5242880000                          {product}
    uintx LargePageHeapSizeThreshold                = 134217728                           {product}
    uintx MaxHeapSize                              := 12582912000                         {product}
     intx ThreadStackSize                           = 1024                                {pd product}
     intx VMThreadStackSize                         = 1024                                {pd product}
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

You can find the following tutorial very useful .

https://www.mkyong.com/java/find-out-your-java-heap-memory-size/

Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68
  • I *strongly* object. While technically correct, this is nothing that you want to do on your system: See my alternative answer. – Olaf Kock Sep 06 '18 at 07:46