In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this article, the editor introduces in detail "how to use the Java virtual machine OOM", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the Java virtual machine OOM" can help you solve your doubts.
The scenario of memory overflow in each runtime region described in the Java virtual machine specification is simulated by code.
First, the virtual machine startup parameters are configured as follows:
-verbose:gc-Xms20M-Xmx20M-Xmn10M-XX:+PrintGCDetails-XX:SurvivorRatio=8 1
Output:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf (Arrays.java:2245) at java.util.Arrays.copyOf (Arrays.java:2219) at java.util.ArrayList.grow (ArrayList.java:213) at java.util.ArrayList.ensureCapacityInternal (ArrayList.java:187) at java.util.ArrayList.add (ArrayList.java:411) at HeapOOM.main (HeapOOM.java:15) Heap def new generation total 9216K, used 8920K [0x32570000, 0x32f70000 0x32f70000) eden space 8192K, 100% used [0x32570000, 0x32d70000, 0x32d70000) from space 1024K, 71% used [0x32d70000, 0x32e26040, 0x32e70000) to space 1024K, 0% used [0x32e70000, 0x32e70000, 0x32f70000) tenured generation total 10240K, used 5693K [0x32f70000, 0x33970000, 0x33970000) the space 1024K, 55% used [0x32f70000, 0x334ff7f8, 0x334ff800, 0x33970000) compacting perm gen total 12288K, used 135K [0x33970000, 0x34570000, 0x37970000) the space 12288K, 1% used [0x33970000, 0x33991dd8, 0x33991dd8, 0x33991e00) 0x33991e00 1024K, 45% 0x33991e00 [0x34570000, 0x34570000, 0x34570000 0x38370000) rw space 12288K, 54% used [0x38370000, 0x389f04f8, 0x389f0600 0x38f70000) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
two。 Virtual machine stack and local method stack overflow
2.1 StackOverflowError
The virtual machine throws a StackOverflowError exception and outputs:
Exception in thread "main" java.lang.StackOverflowError at JavaVMStackSOF.stackLeak (JavaVMStackSOF.java:7) at JavaVMStackSOF.stackLeak (JavaVMStackSOF.java:8)... 1 2 3 4 it is important to note that the more memory allocated to each thread's stack, the more likely it is to produce stack memory overflow exceptions.
It is not difficult to understand that the more each thread is allocated to the stack, the fewer threads can be built, and the easier it is to use up the remaining memory when creating threads.
Therefore, the problem of stack memory overflow can be solved by "reducing memory". / * function description: stack OutOfMemoryError * VM Args:-Xss2M up single thread can use stack space size * @ author zhuyiquan90 * @ created 2017-9-1 11:20:06 * @ version 1.0.0 * @ date 2017-9-1 11:20:06 * / public class JavaVMStackOOM {private void dontStop () {while (true) {}} public void stackLeakByThread () {while (true) {Thread thread = new Thread (new Runnable () {@ Override public void run () {dontStop () }}); thread.start ();}} public static void main (String [] args) {JavaVMStackOOM oom = new JavaVMStackOOM (); oom.stackLeakByThread () }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
3. Method zone and runtime constant pool overflow
JDK 1.7 began to become "de-permanent" gradually. In JDK 1.6 and earlier, since constant pools are allocated in the permanent generation, the size of the method area can be limited by-XX:PermSize and-XX:MaxPermSize, thus indirectly limiting the capacity of constant pools.
Import java.util.ArrayList; import java.util.List / * * function description: run constant pool memory overflow * VM Args:-XX:PermSize=10M-XX:MaxPermSize=10M * @ author author zhuyiquan90 * @ created 2017-9-1 11:50:48 * @ version 1.0.0 * @ date 2017-9-1 11:50:48 * / public class RuntimeConstantPoolOOM {public static void main (String [] args) {/ / use List to maintain constant pool references Avoid Full GC recycling constant pool behavior List list = new ArrayList () / / the PermSize of 10MB is sufficient to generate OOM int i = 0 within the range of integer; while (true) {list.add (String.valueOf (iTunes +) .intern ()) } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
At JDK 1.7 and above, the while loop will continue.
The method area is used to store Class-related information, such as class name, access modifier, constant pool, field description, method description, and so on. For testing in these areas, the basic idea is that the runtime generates a large number of classes to fill the method until it overflows. Package com.suning; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy / * * function description: overflow in the method zone * VM Args:-XX:PermSize=10M-XX:MaxPermSize=10M * @ author author zhuyiquan90 * @ created 2017-9-1 3:31:27 * @ version 1.0.0 * @ date 2017-9-1 3:31:27 * / public class JavaMethodAreaOOM {public static void main (String [] args) {while (true) {Enhancer enhancer = new Enhancer () Enhancer.setSuperclass (OOMObject.class); enhancer.setUseCache (false); enhancer.setCallback (new MethodInterceptor () {@ Override public Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {/ / TODO Auto-generated method stub return proxy.invokeSuper (obj, args);}}); enhancer.create () }} static class OOMObject {}} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
4. Native direct memory overflow
Running result:
Exception in thread "main" java.lang.OutOfMemoryError at sun.misc.Unsafe.allocateMemory (Native Method) at DirectMemoryOOM.main (DirectMemoryOOM.java:14) 1 2 3, this article "how to use Java virtual machine OOM" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.