Several memory overflows and solutions common to Java

Source: Internet
Author: User
Tags xms

The project team has often had some unexpected memory overflow problems in development recently. Let me tell you about some of our common memory overflows!
1.JVM Heap Overflow: Java.lang.OutOfMemoryError:Java heap space
The JVM automatically sets the value of the JVM heap when it is started, and can be set using options such as the-XMN-XMS-XMX provided by the JVM. The size of the heap is the sum of young Generation and tenured generaion. This exception information is thrown in the JVM if 98% of the time is used for GC, and the available heap size is less than 2%.
Workaround: Manually set the size of the JVM heap (heap).
2.PermGen space overflow: Java.lang.OutOfMemoryError:PermGen space
The full name of PermGen space is permanent Generation space, which refers to the permanent storage area of memory. Why memory overflow, this is because the memory is mainly stored by the JVM class and meta information, class in the load is loaded into the PermGen space area, which is stored in the instance heap area, sun The GC does not clean up permgen space during the main program's run time, so if your app loads many classes, there's a good chance that permgen space will overflow. Typically occurs during the startup phase of a program.
Workaround: Set the permanent generation size by-xx:permsize and-xx:maxpermsize.
3. Stack overflow: java.lang.StackOverflowError:Thread stack space
Stack overflows, the JVM is still a stack-based virtual machine, which is the same as C and Pascal. The procedure for calling a function is on the stack and on the fallback. There are too many "layers" of calls to the constructor to overflow the stack area. Generally speaking, the general stack area is much smaller than the heap area, because the function call process is often not more than thousands of layers, and even if each function call requires 1K of space (this is approximately equivalent to a C function declared 256 int type variable), then the stack area is just need 1MB of space. The size of the stack is usually 1-2mb. The popular point is that single-threaded programs require too much memory. Often recursion does not have too many levels of recursion, it is easy to overflow.
WORKAROUND: 1: Modify the program. 2: Use-XSS: To set the stack size for each thread.
4.but has failed to stop it. This was very likely to create a memory leak.
This is generally the start of the program when some timers or other operating threads have not been stopped due to.
Workaround: Implement Servletcontextlistener monitoring and close in the Contextdestroyed method.
5. So when the server container starts, we often care about and set several parameters of the JVM as follows:
The initial size of the-xms:java heap, which defaults to 1/64 of the physical memory.
-xmx:ava Heap Maximum, no more than physical memory.
The heap size of the-xmn:young generation is typically set to one of the 3, 4 points of xmx. Increasing the younger generation will reduce the size of older generations and can be reasonably set according to monitoring.
-XSS: The stack size of each thread, and the best value should be 128K, the default value seems to be 512k.
-xx:permsize: Sets the initial size of the memory's permanent save, the default value is 64M.
-xx:maxpermsize: Sets the maximum size of the memory's permanent save, the default value is 64M.
The ratio of the-xx:survivorratio:eden area to the Survivor area is set to 8, then the ratio of two survivor to one Eden area is 2:8, and a survivor area represents 1/10 of the entire young generation.
-xx:+useparallelgc:f younger generations use concurrent collections, while older generations still use serial collections.
-XX:+USEPARNEWGC: Set the young on behalf of the parallel collection, JDK5.0 above, the JVM will be set according to the system configuration itself, there is no need to set this value.
-xx:parallelgcthreads: The number of threads in the parallel collector, the best value is configured equal to the number of processors for the CMS.
-XX:+USEPARALLELOLDGC: Old Generation garbage collection method for parallel collection (Parallel compacting).
-xx:maxgcpausemillis: The maximum time for each young generation garbage collection (max pause time), if this time is not met, the JVM automatically adjusts the younger generation size to meet this value.
-xx:+scavengebeforefullgc:full called YGC before GC, the default is true.

Real example: java_opts= "-xms4g-xmx4g-xmn1024m-xx:permsize=320m-xx:maxpermsize=320m-xx:survivorratio=6″

All right, here we go! Welcome everyone to pay attention to my blog, if there is doubt, please add QQ Group: 454796847, 135430763 common progress!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Several memory overflows and solutions common to Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.