In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the "Java memory region and memory overflow exception knowledge explanation" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then 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!
Text one. Basic concept
Before we begin to explain, we need to clarify some basic concepts about JVM
As we all know, Java is a cross-platform language, and the basic support of Java cross-platform is actually the shielding of the underlying details of the operating system by JVM, which is equivalent to adding an intermediate layer (any problem in the computer can be solved by adding an intermediate layer). Java is no longer directly translated into machine code for a special platform, like languages such as C _ class +, but into bytecode, that is, our class files. The following figure can probably be summarized more succinctly. Bytecode is equivalent to assembly in the Java world, while JVM is not cross-platform, but JVM on different platforms can recognize and run bytecode files in standard format.
With regard to JVM running the class file, I think the following figure can be expressed more accurately.
What we're going to talk about next is the Runtime Data Area part.
two。 Runtime data area
JVM divides the memory it manages into several different data regions when executing the Java program, as follows:
2.1 Program counter
Thread private
2.1.1 Storage data type
Point to the next bytecode instruction to be executed; if the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; if the Native method is being executed, the counter value is Undefined
2.1.2 abnormal situation
This area is the only one that does not specify any OutOfMemoryError conditions in the Java virtual machine.
2.2 Java virtual machine stack
Thread private
2.2.1 Storage data type
To describe the memory model of Java method execution, each method call corresponds to a stack frame in and out of the stack; a stack frame stores local variables, Operand stack, dynamic link, method exit and other information
The local variable table stores various basic data types known to the compiler, object reference, returnAddress, and the size of the local variable table can be determined during compilation and unchanged during operation.
2.2.2 abnormal situation
StackOverflowError: the thread request stack depth is greater than the virtual machine allowed depth.
Exception sample code:
Public class JavaVMStackSOF {private int stackLength = 1; public void stackLeak () {stackLength++; stackLeak ();} public static void main (String [] args) {JavaVMStackSOF sof = new JavaVMStackSOF (); try {sof.stackLeak ();} catch (Throwable e) {System.out.println ("Stack Length:" + sof.stackLength); throw e }}}
OutOfMemoryError: unable to apply for enough memory when the virtual machine stack is dynamically expanded
Exception sample code:
Public class JavaVMStackOOM {private void dontStop () {while (true) {}} public void stackLeakByThread () {while (true) {new Thread (new Runnable () {@ Override public void run () {dontStop ();}}) .start () }} public static void main (String [] args) {JavaVMStackOOM oom = new JavaVMStackOOM (); oom.stackLeakByThread ();}}
Note: because the memory space allocated by the operating system to each process is limited, if the memory overflow is caused by establishing too many threads, if the number of threads cannot be reduced or the 64-bit virtual machine can be replaced, you can choose to reduce the maximum heap and stack capacity in exchange for more threads.
2.3 Local method stack
Thread private
2.3.1 Storage data types
Similar to the virtual machine stack, except that the local method stack provides Native method services
2.3.2 abnormal situation
StackOverflowError and OutOfMemoryError
2.4 Java heap
Thread sharing
Main areas of garbage collection management
2.4.1 Storage data type
Almost all the object instances are assigned here.
2.4.2 abnormal situation
OutOfMemoryError
Example of exception:
Public class JavaVMHeapOOM {static class HeapOOM {} public static void main (String [] args) {List list = new ArrayList (); while (true) {list.add (new HeapOOM ());} 2.5 method area
Thread sharing
The garbage collection goal in this area is mainly for constant pool collection and type unloading.
2.5.1 Storage data types
Stores data such as class information, constants, static variables, even compiled code that has been loaded by the virtual machine
2.5.2 run-time pool
The runtime pool is part of the method area, but after JDK6, the constant pool is put into the heap
There is also a constant pool section in the Class file, that is, various literals and symbolic references generated at compile time. This part will enter the runtime constant pool in the method area after the class is loaded. In addition, the translated direct references will also be stored in the runtime constant pool.
Another most important feature of the runtime constant pool compared to the Class file constant pool is that it is dynamic, that is, new constants can also be put into the pool during operation, such as String's intern () method.
The String.intern () function is that if the string constant pool already contains a string equal to the String object, the String object representing the string in the pool is returned; otherwise, the string contained in the String object is added to the constant pool and a reference to the String object is returned
Similarly, due to the limit of the collection method area, an OutOfMemoryError is thrown when the constant pool can no longer apply for memory.
2.5.3 abnormal situation
OutOfMemoryError: the method area cannot meet the memory allocation requirements
Example of exception:
Public class RuntimeConstantPoolOOM {public static void main (String [] args) {List list = new ArrayList (); int I = 0; while (true) {list.add (String.valueOf (iTunes +). Intern ());} 2.6 Direct memory
Direct memory is not part of the run-time data area of a virtual machine, but it is also frequently used. For example, NIO class is added to JDK1.4, and an iCandle O mode based on Chanel and Buffer is introduced, which can allocate out-of-heap memory directly using the Native function library, and then operate through a DirectByteBuffer object stored in the Java heap as a reference to this memory. Avoid copying data back and forth in Java heap and Native heap and improve performance
OutOfMemoryError will also be generated.
This is the end of the introduction of "Java memory region and memory overflow exception explanation". 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.