In terms of memory, Java itself has a garbage run mechanism, seemingly we can ignore it, but if we understand it, it might sometimes achieve a multiplier effect.
So take some time to sort out some of this stuff.
I. Basic CONCEPTS
1. Stack
For the understanding of the stack, you can think of it as an empty bucket with only one entrance, there is a principle is "advanced after out" principle, now put things inside, such as put in the order is p,i,n,s,i,l,y, wait, if you want to take out the letter, the order becomes y,l,i,s,n,i,p, As you can imagine, it's a lot easier.
In Java programs, the main purpose of the stack is to store variables generated by the underlying type, such as Int,char and other basic types, but also a function is to hold the object's reference variable, but this does not store the generated object.
2. Heap
The role of the heap in Java is to store objects and arrays that are made by the program new. For example, when a variable stored in the stack is assigned a value, the value is placed in the heap, in short, the execution of the program is the stack and the heap are interrelated.
3. Data sharing
Code: Int a = 3; int b = 3;
When processing, the A variable is processed first. The program in the stack to find there is no variable a, do not have to create a reference variable a, and then in the heap to find there is no 3 this value, do not have to create the allocation of memory to create 3 of the space, a point 3; next process B = 3, create B in the stack, and then find in the heap there is no 3, This is the case of a, a, a shared address, which is called data sharing, so that the memory of the space, to improve the quality of Java operation.
It is important to note, however, that the data in the heap is only co-existing, and only the data in the heap can be shared, because the data in the stack is private and cannot be shared.
4. Static memory
Static memory, refers to the memory size to determine memory, so the memory at the beginning of the memory can be directly based on his size directly allocated space.
5. Dynamic memory
The reason that is called dynamic, is that his size is random, how much to allocate, depending on the circumstances, so, the program starts to run when the mind, can only be used to reach a certain value, immediately before allocating memory to him.
Two. Memory mechanism
1. Basic understanding: It can be imagined as a pointer to C language, to find variables from the stack, pointing to the corresponding address of the variable, that is, their value.
In allocation, static variables are allocated to them directly in the load of the class, but the allocation of dynamic variables is only allocated when they are used.
Java Learning Note (4): Memory management