First of all, thanks to the strong network resources, this blog post is based on the various resources on the network to integrate, and then join their own understanding, may be duplicated with other network resources, hope that other authors forgive. Because beginners Java, if have inaccurate description also ask the reader to correct. The following formally cut to the point:
As we all know, Java and C + + are both object-oriented programming languages, but compared with C + +, Java is easy to get started and easy to use. Little brother to C + + understanding is not many, but one thing is the most headache for C + + beginners, that is, memory management, which is a big difference between C + + and Java. In C + +, memory is managed by programmers themselves, while writing programs can lead to fatal errors ———— crashes, while in Java, Memory is managed by Java virtual machines called garbage collection, and programmers don't have to worry too much about things like that. So there's a lot of time and energy left. But for a good programmer, this mechanism of automatic memory management is not a good thing, just as handmade shoes are much better and more comfortable than the machine-made shoes. Automatic memory management will not only reduce the efficiency of the program, but also reduce the reliability of the program, so Java has been convenient, easy to learn the advantages of sacrificing efficiency and program reliability on the basis of. Since Java memory management is automated, why do we have to understand it for a simple reason: it helps the individual to write more excellent code.
Let's look at the Java memory management mechanism below:
The management of Java memory is nothing more than two points: memory allocation and memory recycling.
The memory allocation includes the heap and the stack: the entities are stored in the heap, and the references to the entities are stored in the stack;
For example: Create a new array int[] arr = new int[3]; in-memory allocations are like this:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5B/64/wKiom1UHu_rBQvhmAACTUcQeXvo501.jpg "title=" The array is stored in memory "alt=" Wkiom1uhu_rbqvhmaactucqexvo501.jpg "/>
Of these, the data in the stack is not persistent. Arr is in the stack, where the address of the array is stored, and once out of scope, ARR is freed and the data in the heap is not freed, but there is no index to get the data for the array. Because ARR is the only key to finding the array data, the key is lost, then the data can not be exploited, it becomes the memory garbage, in C + +, this part of the garbage is the programmer to handle, but in Java has a special garbage collection mechanism, by the virtual machine to complete.
Similarly, when a Java program calls a function, it is loaded into the stack, each function forms a frame, followed by the principle of advanced, after the execution of this part of the memory is released, which is why the function of the local variable is only valid in the scope of the reason.
Figure:data area for Java Virtual runtime
For more detailed information see: http://www.cnblogs.com/gw811/archive/2012/10/18/2730117.html
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5B/64/wKiom1UHw-bCIRS9AAMUfiGQFZw302.jpg "title=" Image_ 5.png "alt=" Wkiom1uhw-bcirs9aamufigqfzw302.jpg "/>
This article is from the "Software Development Exchange" blog, please be sure to keep this source http://smile2015.blog.51cto.com/9250194/1621428
Analysis of Java Memory management