JVM Memory Distribution Summary 1

Source: Internet
Author: User

Java's JVM's memory is mainly divided into 3 extents: heap (heap), stack (stack), and method area (methods) "PS: Secondary: Local method Stack, program counter"

Heap Area:
1. All objects are stored, each containing a class-corresponding information. (The purpose of class is to get operation instructions)
2.JVM only one heap area (heap) is shared by all threads, and the heap does not hold basic types and object references, only the object itself
Stack area:
1. Each thread contains a stack where only objects of the underlying data type are saved and references to custom objects (not objects), objects are stored in the heap area
2. The data in each stack (original type and object reference) is private and other stacks are inaccessible.
3. The stack is divided into 3 parts: the basic type variable area, the execution environment context, the operation instruction area (holds the operation instruction).
Method Area:
1. Also called the static zone, like the heap, is shared by all threads. The method area contains all class and static variables.
2. The method area contains elements that are always unique throughout the program, such as the class,static variable. ps:jdk1.8 The permanent generation has been removed from the method area

So, the basic data types in Java must be stored in the stack.

First,"The basic data types in Java must be stored in the stack." "This sentence must be wrong.

Let's take a look at the reasons below:

Whether the base data type is placed on the stack or in the heap, depending on where the base type is declared, is explained by the storage problem of the data type in memory:

A: The variable declared in the method, that is, the variable is a local variable, each time the program calls the method, the system will establish a method stack for the method, the variables declared in the method is placed in the method stack, when the method end system will release the method stack, corresponding to the method declaration of the variable with the destruction of the stack end, This is why local variables can only be valid in a method

A variable declared in a method can be a variable of a primitive type, or a variable of a reference type.

(1) When the declaration is a basic type of variable, its variable name and value (variable name and value are two concepts) is placed in the method stack

(2) When a reference variable is declared, the declared variable (which is actually stored in the method as a memory address value) is placed in the stack of the method, and the object to which it points is placed in the heap class store.

Two: A variable declared in a class is a member variable, also called a global variable, placed in the heap (because the global variable is not destroyed with the end of a method execution).

A variable declared in a class can be a variable of a primitive type and a variable of a reference type.

(1) When declaring a variable of the base type whose variable name and its value are placed in heap memory

(2) When referencing a type, the variable it declares will still store a memory address value that points to the referenced object. Reference variable names and corresponding objects are still stored in the appropriate heap

In addition, to refute the view that "Java's basic data types are stored on the stack," we can also cite a counter example, for example:

Int[] Array=new int[]{1,2};

Because new is an object, the new int[]{1,2} object is stored in the heap, which means that the two basic data types are stored in the heap,

It's a very effective rebuttal. The basic data type must be stored in the Stack ~ ~ Reference Link: http://www.cnblogs.com/xiohao/p/4296059.html

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.