Java memory overflow and java Overflow
I. There are three common Java memory overflow types:
1. java. lang. OutOfMemoryError: Java heap space ---- JVM Heap (heap) Overflow
JVM automatically sets the value of JVMHeap at startup. The initial space (-Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) cannot exceed the physical memory.
You can use options such as-Xmn-Xms-Xmx provided by JVM to set the configuration. The Heap size is the sum of YoungGeneration and TenuredGeneraion.
In JVM, if 98% is used for GC and the available Heapsize is less than 2%, this exception is thrown.
Solution: manually set the size of the JVMHeap (HEAP.
2. java. lang. OutOfMemoryError: PermGen space ---- PermGen space overflow.
PermGenspace stands for PermanentGeneration space, which is the permanent storage area of the memory.
Why is memory overflow? This is because the memory is mainly used by JVM to store Class and Meta information. When the Class is loaded, it is placed in the PermGenspace region. It is different from the Heap region where the Instance is stored, sun's GC will not clean up PermGenspace during the main program running period. Therefore, if your APP loads many classes, PermGenspace overflow may occur.
Solution: manually set the MaxPermSize
3. java. lang. StackOverflowError ---- Stack Overflow
Stack Overflow. The JVM is still a stack-type virtual machine, which is the same as C and Pascal. The call process of the function is reflected in the stack and rollback.
There are too many "layers" for calling constructors, so that the stack zone overflows.
Generally, the stack zone is much smaller than the heap zone, because the function call process is usually no more than thousands of layers, even if each function call requires 1 K space (which is equivalent to declaring 256 int-type variables in a C function), the stack zone only needs 1 MB space. Generally, the stack size is 1.
2 MB.
Generally, recursion should not involve too many recursive layers, so it is easy to overflow.
Solution: modify the program.
Ii. Solution
In the production environment, if tomcat memory is not properly configured, jvm memory overflow may easily occur.
1. tomcat in linux:
Modify TOMCAT_HOME/bin/catalina. sh
Add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
JAVA_OPTS = "-server-Xms256m-Xmx512m-XX: PermSize = 64 M-XX: MaxPermSize = 128 m"
2. If tomcat 5 is registered as a windows Service and started as a service, you need to modify the corresponding key value in the registry.
Modify the Registry HKEY_LOCAL_MACHINE \ SOFTWARE \ ApacheSoftware Foundation \ Tomcat ServiceManager \ Tomcat5 \ Parameters \ Java, Options on the right
Original Value:
-Dcatalina. home = "C: \ ApacheGroup \ Tomcat5.0"
-Djava. endorsed. dirs = "C: \ ApacheGroup \ Tomcat5.0 \ common \ endorsed"
-Xrs
Add-Xms256m-Xmx512m
Restart the tomcat service and the settings take effect.
3. If tomcat 6 is registered as a windows service or tomcat installation version is used in windows2003,
You can modify it in/bin/tomcat6w.exe.
4. If you want to start tomcat in myeclipse, the above modification will not work. You can set it as follows:
Myeclipse-> preferences-> myeclipse-> servers-> tomcat ×. ×-> In the JDK panel
Add:-Xms256m-Xmx512m-XX: PermSize = 64 M-XX: MaxPermSize = 128 m to OptionalJava VM arguments
Iii. jvm parameter description:
-Server: must be the first parameter. It can be used in multiple CPUs.
-Xms: the initial size of javaHeap. The default value is 1/64 of the physical memory.
-Xmx: maximum value of javaheap. We recommend that you set it to half of the physical memory. The physical memory cannot exceed.
-XX: PermSize: Set the initial size of the permanent memory storage area. The default value is 64 MB. (I used visualvm.exe to view)
-XX: MaxPermSize: sets the maximum size of the permanent memory storage zone. The default value is 64 MB. (I used visualvm.exe to view)
-XX: Invalid vorratio = 2: the size of the survivor pool. The default value is 2. If garbage collection becomes a bottleneck, you can customize the pool settings.
-XX: NewSize: initial size of the new pool. The default value is 2 MB.
-XX: MaxNewSize: maximum size of the newly generated pool. The default value is 32 MB.
If the JVM heap size is greater than 1 GB, the value-XX: newSize = 640m-XX: MaxNewSize = 640 m-XX: Large vorratio = 16 should be used, or allocate 50% to 60% of the total heap size to the newly generated pool. Tune the new object area to reduce the number of FullGC times.
+ XX: AggressiveHeap makes Xms meaningless. This parameter allows the jvm to ignore the Xmx parameter, frantically eat a GB of physical memory, and then eat a GB of swap.
-Xss: the Stack size of each thread. "-Xss15120" means that every thread added to JBoss consumes 15 MB of memory, and the best value is 128 kb, the default value is 512 KB.
-Verbose: gc real-time garbage collection information
-Xloggc: gc. log specifies the garbage collection log File
-Xmn: the heap size of younggeneration, which is usually set to one of the 3 and 4 points of Xmx.
-XX: + UseParNewGC: shorten the minor collection time
-XX: + UseConcMarkSweepGC: shorten the major collection time. This option is more suitable when the Heap Size is large and the Major collection time is long.
-XX: userParNewGC can be used to set parallel Collection [multiple CPUs]
-XX: ParallelGCThreads can be used to increase the degree of parallelism [multiple CPUs]
-XX: After UseParallelGC is set, you can clear the Collector in parallel [multiple CPUs]