Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
java [2012/12/04 23:34]
2a01:260:4121:1:1e6f:65ff:fe38:dc56
java [2012/12/05 00:07]
a [Good/best practices]
Line 1: Line 1:
-====== Java (JVM) tips and tricks ======+====== Java (jvm) tips and tricks ====== 
 + 
 + 
 +===== Enviroment settings ===== 
 + 
 +==== Linux (with multiple java enviroments) ==== 
 + 
 +''.bashrc'' / ''bash.profile'' 
 +   export JAVA_HOME="/usr/local/java/" 
 +   export PATH=/usr/local/java/bin:$PATH 
 + 
 + 
 + 
 +===== Tunning  ===== 
 + 
 +{{ :memoryjvm.png?nolink |}}
  
 ^ switch ^ description ^ ^ switch ^ description ^
-| **-Xmx**   | defines the max memory size that the heap can reach for the JVM. You must know your program well and see how it performs under load and set this parameter accordingly. A low value can cause OutOfMemoryExceptions or a very poor performance if your program's heap memory is reaching the maximum heap size. If your program is running in dedicated server you can set this parameter higher because it wont affect other programs | +| **-Xmx**   **maximum java heap size** - defines the max memory size that the heap can reach for the JVM. You must know your program well and see how it performs under load and set this parameter accordingly. A low value can cause OutOfMemoryExceptions or a very poor performance if your program's heap memory is reaching the maximum heap size. If your program is running in dedicated server you can set this parameter higher because it wont affect other programs | 
-| **-Xms**   | sets the initial heap memory size for the JVM. This means that when you start your program the JVM will allocate this amount of memory instantly. This is useful if your program will consume a large amount of heap memory right from the start. This avoids the JVM to be constantly increasing the heap and can gain some performance there. If you don't know if this parameter is going to help you, **don't use it**.  |+| **-Xms**   **initial java heap size** sets the initial heap memory size for the JVM. This means that when you start your program the JVM will allocate this amount of memory instantly. This is useful if your program will consume a large amount of heap memory right from the start. This avoids the JVM to be constantly increasing the heap and can gain some performance there. If you don't know if this parameter is going to help you, **don't use it**.  | 
 +| **-Xmn** | **the size of the heap for the young generation** -Young generation represents all the objects which have a short life of time. Young generation objects are in a specific location into the heap, where the garbage collector will pass often. All new objects are created into the young generation region (called "eden"). When an object survive is still "alive" after more than 2-3 gc cleaning, then it will be swap has an "old generation" : they are "survivor" . Good size is 33%  | 
 +| **-Xss** | the stack size for each thread | 
 +| || 
 +| **-XX:NewRatio** | the same as Wmn, but using a % (dynamic fs static -Xmn option) **-XX:NewRatio=3** means that the ratio between the old and young generation is 1:3 | 
 +| **-XX:NewSize**  | Size of the young generation at JVM init. Calculated automatically if you specify -XX:NewRatio | 
 +| **-XX:MaxNewSize** | The largest size the young generation can grow to (unlimited if this v alue is not specified at command line) | 
 +| **-XX:SurvivorRatio** | "old generation" called tenured generation, ratio, in %. For example, -XX:SurvivorRatio=6 sets the ratio between each survivor space and eden to be 1:6 (eden is where new objects are created) | 
 +| **-XX:MinHeapFreeRatio** | default is 40%. JVM will allocate memory to always have as minimum 40% of free memory. When -Xmx = -Xms, it's useless. | 
 +| **-XX:MaxHeapFreeRatio:** | default is 70%. The same as Min, to avoid unecessary memory allocation. | 
 + 
 + 
 +<note important> 
 +**A nice gotcha...** \\ 
 +Major collection don't run until tenured is full. 
 +This mean that using -Xmx1024, current heap could be 750MB with 500MB of "dead" object. If the JVM is idle, it could stay like that during a very long time => wasting 500MB or RAM for an idle JVM ! 
 +</note>  
 + 
 +<note tip> 
 +   * It is good practice with server-side Java applications like Resin to set the minimum **''-Xms''** and maximum **''-Xmx''** heap sizes to the same value. 
 +   * For efficient garbage collection, the **''-Xmn''** value should be lower than the **''-Xmx''** value. 
 +</note> 
 + 
 +==== Good/best practices ==== 
 + 
 +FIXME 
 +==== Examples ==== 
 + 
 +tying to limit jvm to ~2G of RAM (java x86_64) 
 +    java -Xms512m -Xmx2G -XX:+UseCompressedOops -server -XX:MaxPermSize128 
 +     
 +java (i386) 
 +    java -Xms1400m -Xmx2G -XX:MaxPermSize=128m -server 
 +==== Troubleshooting ==== 
 + 
 +=== Resolving java.lang.OutOfMemoryError: PermGen === 
 +if you get an error like: 
 + 
 +   java.lang.OutOfMemoryError: PermGen 
 + 
 +The PermGen space is used for things that do not change (or change often). e.g. Java classes.  So often large, complex apps will need lots of PermGen space.  Similarly if you are doing frequent war/ear/jar deployments to running servers like Tomcat or JBoss you may need to issue a server restart after a few deploys or increase your PermGen space. 
 + 
 +To increase the PermGen space use something like: **-XX:MaxPermSize=128m** //(The default is 64MB.)// 
 + 
 +(Note that Xmx is separate from the PermGen space, so increasing Xmx will not help with the PermGen errors). \\ 
 +**The PermGen memory in addition to the Xmx memory.  e.g. -Xmx128m and MaxPermSize=128m would use up to 256MB.** 
 + 
 + 
 + 
 + 
 + 
 + 
 + 
  
  
java.txt · Last modified: 2012/12/07 12:29 by a
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready