JVM Memory Management Overview and Android memory leak analysis
Source: Internet
Author: User
<span id="Label3"></p><p><p>I. Memory partitioning</p></p><p><p><em id="__mceDel">Divides the memory into six parts, namely the PC register, Java Virtual machine stack, Java heap, method area, run constant pool, and local method Stack.<br>1. PC Register (thread exclusive): The full name is the program Count register, which records the address of the Java method that each thread is currently running,<br>If the local method is currently executing, the program counter will be an empty address. Its role is to support multi-threading, thread blocking, recovery,<br>Hang up a series of operations, and intuitively imagine how to recover if you don't remember where each thread is currently running. Based on this,<br>Each thread has a PC register, which means that the PC registers are Thread-unique.</em></p></p><p><p>2. Java Virtual machine stack (thread exclusive): the Java Virtual machine stack is created at the same time that the thread is created, to store the stack frame,<br>Java Virtual machine stacks are also thread-unique.</p></p><p><p>3. Java heap (global share): This is the most important part of Java memory, the most important part,<br>Not because of its importance, but rather as part of what developers should be most concerned About. It is created with the start of the Java virtual machine,<br>All object instances and array objects are stored, and an "automatic memory management system" is built in, which is what we often call garbage collectors (GC).<br>The release of memory in the Java heap is not under the control of the developer and is fully managed by the Java virtual MACHINE. For how Java Virtual machines implement garbage collectors,<br>The Java Virtual Machine specification is not explicitly defined, and because of this, many kinds of garbage collectors are available in the Java virtual machines that we use in Peacetime.<br>They use different algorithms and implementations to meet a variety of performance requirements.</p></p><p><p>4. Method Area (global sharing): The method area is also an integral part of the heap, which mainly stores the Run-time pool, field information, method information,<br>The bytecode content of the construction method and the ordinary function, as well as some special methods. It differs from the Java heap except that the information stored is not the same as the Java heap.<br>The biggest difference is that this part of the Java Virtual Machine specification does not enforce an automatic memory management system (GC).</p></p><p><p>5. Local method Stack (thread exclusive): The local method stack is a traditional stack that is used to support the execution of the native Method.<br>The local method stack is also used when the Java virtual machine is used to implement the instruction set interpreter in other Languages. If none of the preceding two have occurred,<br>This means that if the Java virtual machine does not rely on the local method stack and the Java Virtual Machine does not support the native method, the local method stack is not Required.<br>If necessary, the local method stack is also created with each Thread's Startup.</p></p><p><p>The above five memory areas, in addition to the PC register, the remaining four generally, the Java Virtual machine is required to provide to the customer to adjust the size of the parameters,<br>That is, we often use xms, xmx and so On.</p></p>Java Memory allocation Policy<p><p>Java Program runtime memory allocation policy has three kinds, namely static allocation, stack allocation, and heap allocation, corresponding, three kinds of storage strategy use memory space is mainly static storage (also called method area), stack area and heap Area.</p></p><p><p>Static storage (method area): mainly holds static data, global static data, and Constants. This block exists when the program is compiled, and it is present during the entire run of the Program.<br>Stack Area: When the method is executed, local variables in the method body are created on the stack and the memory held by these local variables is automatically freed at the end of the method Execution. Because the stack memory allocation operation is built into the processor's instruction set, it is highly efficient, but the allocated memory capacity is Limited.<br>Heap Area: also known as dynamic memory allocation, usually refers to the memory that is directly new when the program is Running. The Java garbage collector will be responsible for recycling this part if it is not in Use.<br>Stack-to-heap differences:</p></p><p><p>Variables defined in the method body (local variables) and reference variables of some basic types are allocated in the stack memory of the Method. When a variable is defined in a block of methods, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, the variable is invalidated, and the memory space allocated to it is freed, and the memory space can be reused.</p></p><p><p>Heap memory is used to hold all objects created by new (including all member variables of the Object) and Arrays. The memory allocated in the heap is automatically managed by the Java garbage Collector. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the array or the first address of the object in the heap memory, and this particular variable is the reference variable that we refer to Above. We can use this reference variable to access objects or arrays in the HEAP.</p></p><p><p></p></p>What is a memory leak in Java<p><p>In java, the memory leak is the existence of some assigned objects, these objects have the following two characteristics, first of all, these objects are accessible, that is, in the graph, the existence of the path can be connected to it, and secondly, these objects are useless, that is, the program will no longer use these Objects. If the object satisfies both conditions, these objects can be judged as a memory leak in java, which is not reclaimed by the gc, but it consumes memory.</p></p><p><p>In C + +, memory leaks are larger in scope. Some objects are allocated memory space, and then unreachable, because there is no GC in C + +, the memory will never be returned. In java, these unreachable objects are collected by the gc, so programmers do not need to consider this part of the memory Leak.</p></p><p><p>Through the analysis, we learned that for C + +, programmers need to manage their own edges and vertices, and for Java programmers only need to manage the edge (do not need to manage the release of vertices). In this way, Java improves the efficiency of programming.</p></p><p><p>so, with the above analysis, we know that there is also a memory leak in java, but the scope is smaller than C + +. Because Java is guaranteed by the language that any object is accessible, all unreachable objects are managed by the GC.</p></p><p><p>For programmers, GC is basically transparent and invisible. Although we have only a few functions that can access the gc, such as the function System.GC (), which runs the gc, the function does not guarantee that the Jvm's garbage collector will execute, as defined by the Java language Specification. Because different JVM implementations may use different algorithms to manage the GC. typically, a Gc's thread has a lower priority level. There are a number of policies that the JVM calls the gc, some of which are used to a certain extent, the GC starts to work, there are timed executions, there is a gentle execution of the gc, and some interrupt-execution gc. But generally speaking, we don't need to care about This. Unless the Gc's execution affects the performance of the application on certain occasions, such as a web-based real-time system, such as a network game, where the user does not want the GC to suddenly interrupt application execution for garbage collection, then we need to adjust the Gc's parameters so that the GC can free up memory in a gentle manner. For example, by decomposing garbage collection into a series of small steps, the sun-provided hotspot JVM supports this feature.</p></p><p><p>Also gives a typical example of a Java memory leak,</p></p><pre name="code" class="prettyprint"><pre name="code" class="prettyprint"><code class="hljs vala has-numbering"><span class="hljs-keyword">new Vector(<span class="hljs-number">10);<span class="hljs-keyword">for (<span class="hljs-keyword">int i = <span class="hljs-number">1; i < <span class="hljs-number">100; i++) { <span class="hljs-built_in">Object o = <span class="hljs-keyword">new <span class="hljs-built_in">Object(); v.add(o); o = <span class="hljs-literal">null; }</span></span></span></span></span></span></span></span></span></span></code></pre></pre><p><p>In this example, we iterate over the object and put the requested object into a vector, and if we just release the reference itself, then the Vector still references the object, so the object is not recyclable to the GC. therefore, if the object has to be removed from the vector after it has been added to the vector, the simplest way is to set the vector object to Null.</p></p><p><p></p></p>Common memory leak totals in Android<p><p></p></p><p><p>JVM Memory Management Overview and Android memory leak analysis</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service