Memory Allocation for various data types in jvm, and jvm allocation for Data Types

Source: Internet
Author: User

Memory Allocation for various data types in jvm, and jvm allocation for Data Types
Jvm runtime data zone: the memory managed by JVM is divided into the following runtime data zones: Program counters, Java Virtual Machine stacks, local method stacks, Java stacks, and method zones. This article mainly involves: java Virtual Machine station (java stack for short), java heap and method area. Here, we will briefly popularize the knowledge of these three data zones. The java stack has the fastest reading speed (tightly connected to the CPU register, which is better than the cross-platform register, and android uses registers ), however, the data memory size is allocated during compilation and will not change during runtime. The speed of java stack is not as fast as that of java stack, however, the data memory size in the java heap is not fixed; the first two will be cleared by the java garbage collector. The method area is also called "permanent generation" for Sun HotSpot ", the virtual machine specification allows the region to choose not to implement garbage collection. In contrast, the garbage collection behavior rarely appears in this region, the main aim of memory recycle in this region is to recycle discarded constants and useless classes.
The basic data types are in java. The basic data types include byte, short, int, long, and char ), float, double, and boolean ). The basic data types all have one thing in common, that is, their byte size is fixed, so the memory space they need is also fixed. According to the above jvm introduction, the basic data type is actually put into the java stack, which ensures that the basic data type can be read at the fastest speed, and the basic data type is also used more.
The memory size of an object instance is uncertain when it is referenced, and it often needs to be recycled by the garbage collector, therefore, it is actually stored in the java heap in jvm. For the reference type (that is, Object Reference), its size is fixed, and the reference needs to be quickly found during runtime, it is actually placed in the java stack of jvm. For Object obj = new Object (); there are two implementation methods in the java heap, one is to implement a handle pool in the java heap, pointer placed in the handle pool to the object instance data, that is, the reference in the java stack points to the object instance data pointer in the handle pool in the java stack, and then obtains the instance data; the other is that the handle pool is useless in the java heap, and the reference in the java stack directly points to the instance data in the java heap. Comparison of the two implementation methods: the first implementation has an intermediate process. When the data address of the object instance is moved, the reference in the java stack does not need to be changed. Only the pointer of the handle pool is changed; in the second implementation, a pointer pointing process is missing, and the speed is faster than the first one. However, when the data address of the object instance is moved, the reference in the java stack also needs to be changed. Borrow an image from a blog post: first, second:

String is a special object, and its memory is not used for ordinary object types. First, it is a constant and will not be changed once determined, for example, String str = "erdangjiade" + "" + "blog "; this code will put "erdangjiade", "" and "blog" into the jvm respectively, and then "erdangjiade blog" into the jvm. The reason why StringBuffer is used is that StringBuffer is variable. After talking about this, where is String stored in jvm? If you look for it from Baidu, you will find many misleading answers. strings are actually placed in the String constant pool, the String constant pool is in the constant pool, while the constant pool is in the method area. Therefore, strings are actually placed in the method area. However, string references are stored in the java stack and direct to the constant pool in the method area. If it is new String (""); the String obtained by this method is not put into the String constant pool, but as a normal object placed in the java heap. Example: String str = "erdangjiade "; string newStr = new String ("erdangjiade"); When str = newStr is determined, false is returned because their addresses are different. (Popularize a knowledge: = compares the addresses of two objects, and equal () compares the object data for Equality) the method area also stores class information loaded by virtual machines, such as class type, parent type, interface, method, and method parameters are frequently used in reflection. Therefore, the data stored in the method area is static, basic, and shared.

Please note that
Reference: Lan Ting's [Deep Java Virtual Machine]: Java memory area and memory overflow
A small example of ZangXT: Describes the position of the String constant pool.



In java, what is the difference between the basic data type and the reference data type in memory allocation?

Java memory allocation is mainly divided into four!
One piece is used to install code, that is, compile.
One piece is used to hold static variables, such as variables with static keywords, such as string constants.
One is a stack, that is, a stack. It is used to hold variables and reference types! But the difference is that after a variable is installed, the variable has a value, and the reference type itself has no value on the stack.
One is heap, that is, heap! Heap can be summarized in one sentence to install new things!

To sum up, the basic data types are in the stack, while the reference type and variables are in the stack, and the real content is in the heap, that is, when a new reference type is added, it will be placed in the heap, and the reference type variable in the stack will point to the new thing in the heap!
In this case, we hope to help you understand the java memory allocation problem.

Memory Allocation of basic data types in Java

The basic data type is stored in the memory stack, int [] a = new int [5]. a is put in the stack, and a is just a reference, the address of the array put in the stack, the real array is put in the heap

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.