Android Memory Optimizer 1 Understanding Java memory allocation 1

Source: Internet
Author: User

The opening crap .

Today we are working together to learn the memory allocation of the JVM, the main purpose is to lay the foundation for our Android memory optimization.

have been thinking in what way to present this knowledge point can make us easy to understand, the final decision to use the method is: schematic + source code Analysis .

Welcome to my personal blog: Senduo ' s blog

We would like to be able to write code in the normal time, we can know the current writing code, memory aspects of how to allocate.

We know that a Java programmer in many times do not worry about the release of memory, but rely on the JVM to manage, before writing C + + code, but always remember that the new space to be released in time, or the program is prone to memory overflow situation. Because Java does have a lot of convenience in this area, it gives us more energy to think about business implementations. However, this does not mean that we can use the memory recklessly, because:

1.JVM并不会及时的去清理内存2.我们无法通过代码去控制JVM去清理内存

This requires that we usually in the development process, to understand the JVM garbage collection mechanism, reasonable arrangement of memory.

So how can we properly arrange the memory? Then we need to understand the JVM's memory allocation mechanism before we can really control it so that the program runs in our applause.

Technical Details1.JVM memory model

Usually we have a relatively rough concept of Java memory, that is, the heap and stack, but it is actually much more complex, the following gives the full memory model:

Memory model

The content of the corresponding region is:

Content Model 1.1 program counter PC

I have outlined the following points in this area:

1.这一区域不会出现OOM(Out Of Memory)错误的情况2.属于线程私有,因为每一个线程都有自己的一个程序计数器,来表示当前线程执行的字节码行号3.标识Java方法的字节码地址,而不是Native方法4.处于CPU上,我们无法直接操作这块区域
1.2 Virtual Machine Stack

This area is also the stack of stacks that we usually say about this block area, which has the following points:

1.属于线程私有,与线程的生命周期相同2.每一个java方法被执行的时候,这个区域会生成一个栈帧4.栈帧中存放的局部变量有8种基本数据类型,以及引用类型(对象的内存地址)5.java方法的运行过程就是栈帧在虚拟机栈中入栈和出栈的过程6.当线程请求的栈的深度超出了虚拟机栈允许的深度时,会抛出StackOverFlow的错误7.当Java虚拟机动态扩展到无法申请足够内存时会抛出OutOfMemory的错误
1.3 Local Method Stack

This area, which belongs to the thread-private, as the name implies, differs from the virtual machine stack, which is used to process the native method (Java native method), and the virtual machine stack is the Java method. For the native method, there are many native methods in object, hashcode,wait and so on, the execution of these methods is often aided by the operating system.

This area is also likely to throw Stackoverflowerror and OutOfMemoryError

1.4 Java Heap

We usually say the most, the most concerned about a region, it is him. The performance optimizations we perform later are mainly for this part of memory, the main battlefield of the GC, and this place holds almost all the object instances and array data. Here I've probably made the following generalizations:

1.Java堆属于线程共享区域,所有的线程共享这一块内存区域2.从内存回收角度,Java堆可被分为新生代和老年代,这样分能够更快的回收内存3.从内存分配角度,Java堆可划分出线程私有的分配缓存区(Thread Local Allocation Buffer,TLAB),这样能够更快的分配内存4.当Java虚拟机动态扩展到无法申请足够内存时会抛出OutOfMemory的错误
1.5 Method Area

The method area mainly stores data such as class information, constants, static variables, compiler-compiled code, etc. that have been loaded by the virtual machine. The GC appears less in the region. Summarized as follows:

1.方法区属于线程共享区域2.习惯性加他永久代3.垃圾回收很少光顾这个区域,不过也是需要回收的,主要针对常量池回收,类型卸载4.常量池用于存放编译期生成的各种字节码和符号引用,常量池具有一定的动态性,  里面可以存放编译期生成的常量5.运行期间的常量也可以添加进入常量池中,比如string的intern()方法。
1.6 Running a constant-rate pool

The run-time-constant pool is also part of the method area, which holds the various literal and symbolic references generated by the compiler. To explain it alone is because we usually use string to compare more, involving this piece of knowledge, but this piece of area will not throw OutOfMemoryError

2.JVM Memory Source Sample Description

First write a main method, to do a demonstration, the code is as follows:

Package senduo.com.memory.allocate;/** * ***************************************************************** * * File Ouyangshengduo * * Created: 2017/8/11 * * File Description: Memory allocation Call Procedure Demo code * * Revision History: 2017/8/11 9:39************************************* * * /public class Memoryallocatedemo {public static void main (string[] args) {//JVM automatically finds the Main method/** * Executes the first sentence of code                , create a test instance test, allocate a block of memory to the stack, and hold a pointer to the heap instance object */test test = new Test ();                /** * Executes the second sentence, declares a variable of type int (8 basic data types), allocates a block of memory directly in the stack, stores the value of the variable */int date = 9; /** * Executes the third sentence, creates a BirthDate instance bd1, allocates a chunk of memory in the stack, holds a pointer to the heap instance object */BirthDate BD1 = new BirthDate (13,6,19                91); /** * Executes the fourth code, creates a BirthDate instance bd2, allocates a chunk of memory in the stack, holds a pointer to the heap instance object */BirthDate Bd2 = new BirthDate (30,4,19                91);                /** * Executes the fifth sentence code, the method test1 into the stack frame, executes the stack */test.test1 (date); /** * Executes the sixth sentence code, the method test2 into the stack frame, executes the stack */test. Test2 (BD1);    /** * Executes the seventh sentence code, the method test3 into the stack frame, executes the stack */TEST.TEST3 (BD2); }}
Demonstration process One
1.JVM自动寻找main方法,执行第一句代码,创建一个Test类的实例test,  在栈中分配一块内存,存放一个指向堆区对象的指针110925。2.创建一个int型的变量date,由于是基本类型,直接在栈中存放date对应的值9。3.创建两个BirthDate类的实例bd1、bd2,在栈中分别存放了对应的指针指向各自的对象  ,他们在实例化时调用了有参数的构造方法,因此对象中有自定义初始值。  

The illustrations are as follows:

Memory allocation call Demo a demo process two
1.test1方法入栈帧,以date为参数2.value为局部变量,把value放在栈中,并且把date的值赋值给value3.把123456赋值给value局部变量4.test1方法执行完,value内存被释放,test1方法出栈
Memory allocation call demo two memory allocation call demo two memory allocation call demo two demo process three
1.test2方法入栈帧,以实例bd1为参数2.birthDate为局部变量,把birthDate放在栈中,把bd1的引用的值赋值给birthDate,  也就是bd1与birthDate的地址都是指向同一个堆区的实例3.在堆区new了一个对象,并且把这个堆区的指针保存在栈区中birthDate对应的内存空  间,这个时候,bd1与birthDate指向了不同的堆区,那么birthDate的改变,并不会对  bd1造成影响4.test2方法执行完,栈中的birthDate空间被释放,test2方法出栈,但堆区的内存空间  则要等待系统自动回收
Memory allocation call demo three memory allocation call demo three memory allocation call demo three demo process four
1.test3方法入栈帧,以实例bd2为参数2.birthDate为局部变量,把birthDate放在栈中,把bd2的引用的值赋值给birthDate,  也就是bd2与birthDate的地址都是指向同一个堆区的实例3.调用birthDate的setDay方法,因为birthDate与bd2指向的是同一个对象,也就是bd2调用了setDay方法,所以,也会bd2造成影响4.test3方法执行完,栈中的birthDate空间被释放,test3方法出栈
Memory allocation call demo four memory allocation call demo four memory allocation call demo 43. JVM Memory Allocation Summary

Follow the above four steps, walk again, will find in fact also not so complex, master thought can touch the road, we usually pay attention to distinguish the basic data types of variables and reference data type variables, the following is a few summary:

1.局部变量中的基本数据类型的值直接存栈中2.局部变量中的引用数据类型在栈中存的是引用类型的指针(地址)3.栈中的数据与堆中的数据内存回收并不是同步的,栈中的只要方法运行完,就会直接  销毁局部变量,但堆中的对象不一定立即销毁  4.类的成员变量在不同对象中各不相同,都有自己的存储空间(成员变量在堆中的对象中  )。而类的方法却是该类的所有对象共享的,只有一套,对象使用方法的时候方法才被  压入栈,方法不使用则不占用内存
Dry Goods Summary

Finally, the sharing of the JVM's memory allocation has been written down and written down, and indeed a deep understanding of memory allocation. The following blogs are referenced during the period:

Java's beauty [from rookie to master evolution] JVM memory management and garbage collection

A comprehensive analysis of Java memory allocation

JVM Memory model

After understanding the JVM memory model, the next chapter will explore the JVM garbage collection mechanism.

Android Memory Optimization 1 Understanding Java memory allocation 1

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.