Java memory management notes

Memory model

1. PC Program Counter Register Register

Strictly speaking is a data structure that holds the memory address of the program's current normal execution, to ensure that only can continue execution when an interrupt.

According jvm specification, in this region does not throw OutOfMemoryError memory exception. 


2. Java stack Java stack

This area is the memory area most prone to abnormal, each thread corresponding to a thread stack to generate, for each thread executes a method, that they will create a stack frame (Frames), for storing local variable table method, the stack operation, the dynamic connecting, the method returns information value and the like.

jvm specification defines two regions of the memory exception, OutOfMemoryError, StackOverflowError. 

Command settings:

-Xss


3. The native method stacks Native Method Stack

And the same VM stack, the difference is not the same object processing, stack processing java virtual machine byte code, and the local Native method stack is processed. Other aspects of the agreement.

Command settings:

-XX:PermSize=2M

-XX:MaxPermSize=2M

 

4. Heap Heap

Heap can be accessed by all threads, with the start of the virtual machine exists, this great area, because all threads are instantiated object is stored in this area, because each type, each interface class needs the memory is not the same, multiple branches within a method requires memory are not the same, we are only at run time in order to know how many objects to be created, how much address space needs to be allocated.

GC concern is the part of this, so often also referred to as GC heap heap.

Heap certainly not throw StackOverflowError types of exceptions, so only the relevant type OutOfMemoryError exception. 

Command settings:

-Xms (minimum heap) 

-Xmx (maximum heap)


Method Area The method area

Used to store class information has been loaded in the virtual machine, constants, static method, even after the compiled code.

The same can throw OutOfMemoryError related types of exceptions.


Memory Problems

1. The memory overflow out of memory

Definition: refers to the program at the time of application memory, there is not enough memory space for its use, appear out of memory; for example, apply for an integer, but to save it in order to save enough for a few long, and that is out of memory.

the reason:

  • The amount of data loaded in memory is too large, too much data as extracted from a database;
  • Class has a collection of references to objects, not emptied after use, can not be recycled so that the JVM;
  • Infinite loop or cycle is repeated excessive physical presence of the object code;
  • Third-party software used in the BUG;
  • Startup parameter memory value is set too low.


2. The memory leak memory leak

It refers to the program after the application memory to free up memory space has been filed, a memory leak hazards can be ignored, but the accumulation of memory leaks very serious consequences, no matter how much memory, sooner or later occupied light.


reference:

1. "In-depth analysis of Java Web technology insider" promise to make waves

2. "Java programming explain high concurrency" Wang Wenjun

3. "memory overflow Solutions"  www.cnblogs.com/200911/p/39...





Guess you like

Origin juejin.im/post/5dc145916fb9a04a5d5864cb