1. Classification of memory Area
Stack memory: Reference to basic type variables and objects with the advantage of faster access
Heap Memory: Objects and arrays created by new and instantiation variables of objects, with the advantage of dynamically allocating memory, but with relatively slow access speed
2. Different types of memory allocations
(1) Basic data type: data types such as Int,char,double
When using int A; To define, a space is allocated in the stack space to save a (at this time empty); When using int a=2, it allocates a space in the stack space to save a, and to find out if there is no value of 2 memory space, if there is the address space of a point to 2 address space, If you do not create an address space with a value of 2, point the address space of a to that address space.
(2) Object
The memory allocation of an object is primarily the address of the object in the stack space (the address of the corresponding heap space), where the object is stored in the heap space
(3) String
String is actually a class, but there are more special places.
string s = new String ("hello,world!"); and String s = "hello,world!"; is not the same:
The former is the same as the ordinary class, the latter is similar to the basic type, will first go to find there is no "hello,world!", if not only to create a value of "hello,world!" The object, and then the stack space holds the address of this object in the heap (the address of this stack space is x,x not the address of the object in the heap), and the value of s in the stack space is x.
(4) array
The stack holds the array at the first address in the heap, and the array is allocated in the heap.
(5) Memory allocation at method invocation
instance variables and objects in the heap, local variables in the stack (after the execution of the method is completed, it is recycled)
3. Garbage collection mechanism
(1) mode
The JVM recycles itself or is System.GC () and requests a recycle.
(2) Object
1) null reference: String s = null, at which time S is recycled
2) No reference: String s = "a"; s = "B"; then a becomes "the Ghost of the Lord" and will be recycled.
3) Expiration: When the method is finished, the local variables in the method are recycled
4) referencing each other: Object A refers to object b,b directly or indirectly referencing object A, and a and B are not referenced by other objects, and are also recycled
(3) Garbage collection algorithm
http://speed847.iteye.com/blog/373278
The memory management mechanism of Java