Memory allocation of various data types in jvm

Source: Internet
Author: User
A brief introduction to the jvm runtime data zone & #160; & #160; & #160; 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. & #160; & #160; This article mainly involves: java virtual machine station (java stack), java heap, and method zone. & #160; & #160; here is a brief introduction to the 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.


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.