Java memory Parsing

Source: Internet
Author: User

Although stacks, stacks, and constant pools belong to the same Java memory allocation operation region, their applicability and functions are quite different. This article will go deep into the Java core and briefly explain Java memory allocation knowledge.

First, we will explain each area in the memory.

Stack: stores basic types of data and object references, that is, stores variables.

If the data of the basic type (non-static variables) is stored, the variable name and value are directly stored in the memory of the stack;

If it is a reference type, the variable name is stored in the stack and then points to the new object (stored in the heap ).

Heap: stores new data.

Data segment: divided into static and constant fields.

Static domain:

Static members defined in static are stored in the object (I .e. static variables. Similarly, if the static variable is of the basic type, the variable names and values are stored in the static domain, if it is a reference type, it points to the new object ).

Constant pool:

A constant pool refers to some data that is determined during the compilation period and stored in the compiled. class file. Besides containing the basic types (such as int and long) defined in the Code and the constant values (final) of the object type (such as String and array) it also contains some symbolic references that appear in the form of text, such as fully qualified names of classes and interfaces, field names and descriptors, methods and names and descriptors.

Code segment: stores the code.

 

Here are a few simple examples to show you the specific content stored in these regions:

Note: In Examples 1 and 2, you should note that the String type is not a basic data type, so it can create an object and put its value in the constant pool.

Example 1. Creation of non-static and static variables:

String str = "abc"; static Int a = 10; // create a String-type variable str; create an Int-type variable

Memory parsing:

Here, str is a non-static variable, so it is put into the stack, while static variable a needs to be put into the static domain, for variable values "abc" and 10 should be put into the constant pool.

 

 

Example 2. Object creation:

String str = new String ("abc"); // create a String-type object str

Memory parsing:

Here, str is a String type object, so it is put into the stack, and the new stuff is put into the heap, and the initial value "abc" is put into the constant pool.

 

 

Example 3. Create an array

Int s [] = new Int [3]; s [0] = 3; s [1] = 2; s [2] = 3;

Memory parsing:

Here s [] is an array variable, so it is put into the stack, new items are still put into the heap, and the value of each last element is put into the 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.