Understanding the benefits of the Java Virtual Machine memory mechanism
When we need to solve some tricky problems, such as performance optimization, to solve the oom anomaly, if we understand the memory mechanism of the Java Virtual machine, we know where to begin to deal with these problems.
How to understand me from three aspects to explain his
Process level We do an app on the PC and make an app on the phone that exists at the level of the process. At this level of the process, memory is allocated/allocated to the Java heap, the method area (containing the constant store), the individual threads
- Java heap: The Java heap is the area where GC threads reclaim memory, and he is primarily used to store various objects
- Method Area: The method area stores the information after the class is loaded, such as global variables, methods, etc.
- Constant store: The static variable store is included in the method area and is used to hold various static final constants, which are mutable
In addition to the Java heap and method areas, other memory spaces are allocated to individual threads.
There is a possibility that an oom exception will occur in these memory areas.
Thread level
- Program counter: Each thread will have a program counter, this in debug when the track to go to which step. The program counters use very little memory, and this part does not produce OOM exceptions!
- Java Virtual machine stack: As the name implies, the Java Virtual machine stack is in the form of a stack, so he accesses more quickly than the heap. The Java Virtual machine stack stores local variables, Java's basic data types, and object type/array types store addresses that point to the Java heap
- Native method Stack: The native method stack corresponds to the contents of the native method stored in the Java Virtual machine stack
Stack and heap are different, stacks will throw stackoverflowexception, but also may throw oom, but in combat, stack memory exceeded the memory limit, generally throw is StackOverflowException
Other
- Direct Memory: Direct memory is used in NIO, in order to avoid native heap and local heap copy data back and forth, and the implementation of the technology
Above, if there are errors, ask the great gods to correct them.
[Java Basics] Java Virtual machine memory mechanism