JAVA heap region / stack area / method area

register:

The fastest storage area is located inside the processor, but the number is extremely limited. Therefore, according to the demand register is automatically assigned, no direct human control

Heap:

Among located in RAM, a common memory pool. Wherein the stored data is managed automatically by the JVM.

jvm only a heap (heap) is shared by all threads , only to store the object itself. Also known as dynamic memory allocation, when you need an object, use the new writing a single line of code, when executing this line of code will automatically allocate storage on the heap. After use, will be dedicated to the recovery of the garbage collector is idle.

1. Java heap is shared by all threads in a memory area created when the virtual machine is powered on, the memory is the biggest piece of virtual machine management. The sole purpose of this memory area is stored object instance and an array of [], almost all object instances and arrays are all here to allocate memory.

2. Java heap is the main area managed by the garbage collector, also known as GC garbage. It will be devoted to the analysis behind the GC algorithm.
From the memory recovery of view, since the collector is now basically using generational collection algorithm, Java heap may be subdivided into: the new generation, the old generation;
from the point of view of memory allocation, threads share the Java heap may be divided into a plurality of thread private allocation of buffer (TLAB);
regardless of division, has nothing to do with the content store, regardless of which area, storage is still the object instances and arrays.

3. If there is no complete examples in the heap memory allocation, and the heap can no longer expand, it will throw an OutOfMemoryError.

4. memory leaks and memory overflow
memory leaks: refers to the program dynamically allocated memory to some temporary objects, but objects can not be recovered GC, it always take up memory. Ie objects allocated up to but no longer use, available memory less and less.
Memory overflow: refers to the program is running can not apply enough memory caused by a mistake. Memory overflow usually occurs in older years or permanent generational garbage collection, the memory is still no room for new Java objects circumstances.
Memory leak is a cause of memory leaks, is not the only factor.

Stack area:

It located in RAM which can be obtained directly from the processor supported by the stack pointer. Downward movement of the stack pointer, allocating new memory; has moved upward, to release that memory. This speed after storage register.

  And object references for storing basic data type (note that no String)

  When the method is performed in vivo method of local variables (basic data including a reference type, object) are created on the stack.

  Because the stack memory allocation operation in the processor instruction set, high efficiency, but the limited amount of memory allocated.

  After use, recycle immediately.

Static storage area (area method):

The main store static data, static data and global constants. In this memory has been allocated the program compile time is good, and there during the entire program run.

1. The method area is also known as a static area, the program is always the only element storage area. And the heap, is the various threads of shared memory area. It is used to store class information has been loaded virtual machine, constants, static variables, the time compiler to compile the code and other data.

2. Java Virtual Machine Specification limitation on the method very relaxed area, in addition to and do not require continuous as the Java heap memory can be selected and fixed size or may be expanded, you can also choose not to implement garbage collection.
Garbage collection target this area is mainly for recycling and type of constant pool of unloading, in general, memory recovery in this region is relatively unsatisfactory, especially the type of recovery, conditions can be quite harsh, but this part of the region's garbage collection indeed necessary.

3. Many developers prefer to method area called "permanent Generation" (Perm Gen) (Permanent Generation) "Always hold the content will not easily change." In the HotSpot JDK 1.7 is now released, had been put on permanent generation string constant pool moved to the heap.

4. The runtime constant pool (Runtime Constant Pool) is part of the zone method.

Stack and heap differences:

  Storage data type : stack area stores basic data variables (note not including String) and reference data variables (created with the new keyword), heap memory is used to store all the objects created by the new (including all the member variables of these objects) and arrays

  You can define a special stack variable, the value of this variable array or object is equal to the first address in the heap memory, this particular variable is what we said above, reference variables . We can use this reference variable to access objects in the heap or array.

  Storage speed: In terms of memory speed, memory allocation and stack memory faster to clean the stack, and the stack memory store located directly behind speed among the processor registers.

  Flexibility: in terms of flexibility, due to the different stack memory and memory storage mechanism heap, heap memory flexibility and more than stack memory. Flexibility: in terms of flexibility, due to the different stack memory and memory storage mechanism heap, heap memory flexibility and more than stack memory.

Guess you like

Origin www.cnblogs.com/alex-xyl/p/11298555.html