Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
java [2012/12/03 12:25] a createdfdsfwqw |
java [2012/12/06 17:28] 194.249.198.48 Added garbage collector part |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Java (JVM) tips and tricks ====== | + | ====== Java (jvm) tips and tricks ====== |
+ | |||
+ | |||
+ | ===== Enviroment settings ===== | ||
+ | |||
+ | ==== Linux (with multiple java enviroments) ==== | ||
+ | |||
+ | '' | ||
+ | | ||
+ | | ||
+ | |||
+ | |||
+ | |||
+ | ===== Tunning | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ^ switch ^ description ^ | ||
+ | | **-Xmx** | ||
+ | | **-Xms** | ||
+ | | **-Xmn** | **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 allocated 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 " | ||
+ | | **-Xss** | ** Stack size for each thread ** - This sets size of preallocated stack for each thread. If you set this too low your program will crash with StackOverflowExceptions. Setting this too high will cause excessive memory usage - this amount of memory gets allocated for **each** thread in the JVM. The default for most JVMs is 512k. Note: When increasing this number on Linux, you will probably have to also increase native stack size to same amount with " | ||
+ | | || | ||
+ | | **-XX: | ||
+ | | **-XX: | ||
+ | | **-XX: | ||
+ | | **-XX: | ||
+ | | **-XX: | ||
+ | | **-XX: | ||
+ | |||
+ | |||
+ | <note important> | ||
+ | **A nice gotcha...** \\ | ||
+ | Full-heap major garbage collection **will not** run until the tenured part of heap is full. This means if you have heap size (Xmx) of 1024MB with 750MB of heap allocated and your program has only 250MB of objects actually reachable (" | ||
+ | |||
+ | This is by design - it improves performance. Don't give JVM more heap space than you're actually willing to lose. | ||
+ | </ | ||
+ | |||
+ | <note tip> | ||
+ | * It is good practice with server-side Java applications like Resin to set the minimum **'' | ||
+ | * For efficient garbage collection, the **'' | ||
+ | </ | ||
+ | |||
+ | ===== Choosing the garbage collector ===== | ||
+ | |||
+ | ==== Good/best practices ==== | ||
+ | |||
+ | 1. OpenJDK 1.6 and 1.7 may (as of now, 6. 12. 2012) still be unstable in stressful production environments leading up to segmentation faults and unintended behaviour. Use Sun/Oracle JVM for maximum stability and performance. | ||
+ | |||
+ | |||
+ | ==== Examples ==== | ||
+ | |||
+ | tying to limit jvm to ~2G of RAM (java x86_64) | ||
+ | java -Xms512m -Xmx2G -XX: | ||
+ | |||
+ | java (i386) | ||
+ | java -Xms1400m -Xmx2G -XX: | ||
+ | ==== Troubleshooting ==== | ||
+ | |||
+ | === Resolving java.lang.OutOfMemoryError: | ||
+ | if you get an error like: | ||
+ | |||
+ | | ||
+ | |||
+ | The PermGen space is used for things that do not change (or change often). e.g. Java classes. | ||
+ | |||
+ | To increase the PermGen space use something like: **-XX: | ||
+ | |||
+ | (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. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
- | | switch | description | | ||
- | | -Xmx | max memory | ||
- | | -Xms | min memory | ||