In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "memory overflow exception description of JVM". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Give an example to illustrate the meaning:
-Xss128k
The java stack size of each thread, the allowed size of 128k for the sum of all stack frame sizes of a thread java stack.
-Xms128m
Represents the minimum size of JVM Heap (heap memory) 128MB, initial allocation
-Xmx512m
Represents the allowed size 256MB of JVM Heap (heap memory) * *, allocated as needed.
-XX:PermSize=20M
Set the initial size of the method area
-XX:MaxPermSize=30M
Set the * value of the method area
Java stack overflow
In the Java virtual machine specification, two exception conditions are specified for this area: StackOverflowError and OutOfMemoryError exceptions.
1.StackOverflowError exception
Each time the java program code starts a new thread, the Java virtual machine allocates a Java stack to it. The Java stack holds the running state of the thread in frames. When a thread calls the java method, the virtual machine pushes a new stack frame into the thread's java stack. As long as this method has not been returned, it always exists. If there are too many levels of method nesting calls in a thread (such as recursive calls), with the gradual increase of frames in the java stack, the StackOverflowError memory overflow exception will eventually occur because the sum of all the stack frames in the java stack of the thread is greater than the value set by-Xss. Examples are as follows:
/ * VM Args:-Xss128k * / public class Test {private int count = 0; public static void main (String [] args) {new Test () .method ();} public void method () {System.out.println (+ + count); method ();}}
-Xss is 128k. One of the test results is that when the value of count adds up to 2312, the following exception occurs:
Exception in thread "main" java.lang.StackOverflowError at sun.nio.cs.UTF_8.updatePositions (UTF_8.java:58) at sun.nio.cs.UTF_8 $Encoder.encodeArrayLoop (UTF_8.java:392) at sun.nio.cs.UTF_8 $Encoder.encodeLoop (UTF_8.java:447) at java.nio.charset.CharsetEncoder.encode (CharsetEncoder.java:544) at sun.nio.cs.StreamEncoder.implWrite (StreamEncoder.java: At sun.nio.cs.StreamEncoder.write (StreamEncoder.java:106) at java.io.OutputStreamWriter.write (OutputStreamWriter.java:190) at java.io.BufferedWriter.flushBuffer (BufferedWriter.java:111) at java.io.PrintStream.write (PrintStream.java:476) at java.io.PrintStream.print (PrintStream.java:547) at java.io.PrintStream.println (PrintStream.java:686) at jvm.Test.method (Test.java:17)
Modify-Xss to 1280k. One of the test results is that a StackOverflowError exception occurs when the value of count adds up to 26888. As the value of the-Xss parameter increases, so does the level of method calls that can be nested.
To sum up, the StackOverflowError exception is due to the deep level of the method call, resulting in the sum of all stack frames allocated to a thread greater than the value set by-Xss, resulting in a StackOverflowError exception.
2.OutOfMemoryError exception
When the java program code starts a new thread, there is not enough memory space to allocate the java stack for the thread (the size of a thread's java stack is determined by the-Xss parameter), and jvm throws an OutOfMemoryError exception. Examples are as follows:
/ * VM Args:-Xss128k * / public class Test {public static void main (String [] args) {int count = 0 While (true) {Thread thread = new Thread (new Runnable () {public void run () {while (true) {try {Thread.sleep (5000) } catch (Exception e) {}}); thread.start (); System.out.println (+ + count);}
-Xss is 128k. One of the test results is that when the value of count adds up to 11887, the following exception occurs:
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0 (Native Method) at java.lang.Thread.start (Thread.java:640) at jvm.Test.main (Test.java:20)
Modify-Xss to 1280k. One of the test results is that an OutOfMemoryError exception occurs when the value of count adds up to 1270. As the value of the-Xss parameter increases, the less the total number of threads that the java program can create.
Java heap overflow
The Java heap is used to store object instances. When you need to allocate memory for the object instance, and the memory footprint of the heap has reached the * * value set by-Xmx. An OutOfMemoryError exception will be thrown. Examples are as follows:
/ * VM Args:-Xmx5m * / public class Test {public static void main (String [] args) {int count = 0; List list = new ArrayList (); while (true) {list.add (new Object ()); System.out.println (+ + count);}
-Xmx is 5m. One of the test results is that when the value of count adds up to 297868, the following exception occurs:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf (Arrays.java:2760) at java.util.Arrays.copyOf (Arrays.java:2734) at java.util.ArrayList.ensureCapacity (ArrayList.java:167) at java.util.ArrayList.add (ArrayList.java:351) at jvm.Test.main (Test.java:15)
Modified-Xmx is 10m. One of the test results is that when the value of count adds up to 670205, an OutOfMemoryError exception occurs. As the value of the-Xmx parameter increases, more objects can be stored in the java heap.
Method zone overflow
The method area is used to store information about java types, such as class names, access modifiers, constant pools, field descriptions, method descriptions, and so on. When the class loader loads the class file into memory, the virtual machine extracts the type information from it and stores it in the method area. When you need to store class information and the memory footprint of the method area has reached the * * value set by-XX:MaxPermSize. An OutOfMemoryError exception will be thrown. For testing in this case, the basic idea is to generate a large number of classes at run time to fill the method area until it overflows. Here we need to directly manipulate the bytecode runtime with the help of CGLib to generate a large number of dynamic classes. Examples are as follows:
/ * VM Args:-XX:MaxPermSize=50M * / public class Test {public static void main (String [] args) {int count = 0; while (true) {Enhancer enhancer = new Enhancer (); enhancer.setSuperclass (Test.class); enhancer.setUseCache (false) Enhancer.setCallback (new MethodInterceptor () {public Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {return proxy.invoke (obj, args);}}); enhancer.create () System.out.println (+ + count);}
-XX:MaxPermSize is 50m. One of the test results is that when the value of count adds up to 3953, the following exception occurs:
Caused by: java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1 (Native Method) at java.lang.ClassLoader.defineClassCond (ClassLoader.java:631) at java.lang.ClassLoader.defineClass (ClassLoader.java:615)... 8 more
Modified-XX:MaxPermSize is 100m. One of the test results is that when the value of count adds up to 8022, an OutOfMemoryError exception occurs. As the value of the-XX:MaxPermSize parameter increases, more type data can be stored in the java method area.
This is the end of the description of the memory overflow exception of JVM. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.