Java memory model

Source: Internet
Author: User

Basic data type:

Basic data types Size Range Default value
int (shaping) 32 -2147483648~2147483648 0
Long (length shaping) 64 -9233372036854477808~9233372036854477808 0
Float (float type) 32 -3.40292347e+38~3.40292347e+38 0.0f
Double (dual precision) 64 -1.79769313486231570e+308~1.79769313486231570e+308 0.0d
Short (shorter integer type) 16 -32768~32768 0
Boolean (Boolean) 1 True/false False
char (character type) 16 ' \u0000-u\ffff ' ' \u0000 '
Byte (byte type) 8 -128~127 0

Reference data type:

Class, interface type, array type, enumeration type, annotation type.

Difference:

When the base data type is created, it is partitioned into a piece of memory on the stack, and the values are stored directly on the stack.

When a reference data type is created, it first allocates a piece of memory to its reference (handle) on the stack, and the object's specific information is stored on the heap memory, and then the reference above the stack points to the address of the object in the heap.

For example, there is a class person, with attribute Name,age, with the constructor method of the argument,

Person p = new person ("Zhangsan", 20);

The specific creation process in memory is:

1. First in the stack memory bit its p allocated a space;

2. Allocate a block of space to the person object in heap memory and set the initial value "" for its three attributes, 0;

3. According to the definition of the property in the class person, the object's two attributes are assigned operation;

4. Call the construction method and assign a value of two to "Tom", 20; (Note that there is no connection between p and person objects at this time);

5. Assign the address of the person object in the heap memory to p in the stack, and the reference (handle) p to find specific information about the objects in the heap.

Related knowledge:

static zones: saves automatic global variables and static variables (including static global and local variables). The contents of the static zone are present throughout the lifetime of the program, and are allocated by the compiler at compile time.

Heap Area: The memory allocated by the MALLOC series function or new operator, typically released by the programmer, whose life cycle is determined by either free or delete. exists until it is released, until the program ends and is released by the OS. It is characterized by the use of flexible, large space, but error-prone

Stack Area: automatically allocated by the compiler to release, save local variables, the contents of the stack only within the scope of the function, when the function is finished, the content will be automatically destroyed, characterized by high efficiency, but limited space size

literal constant area: The constant string is put here. Released by the system after the program is finished.

Program code area: binary code that holds the body of the function.

Java memory model

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.