Java memory mechanism details, Java memory mechanism details

Source: Internet
Author: User

Java memory mechanism details, Java memory mechanism details

Java divides memory into two types: stack memory and heap memory. Some of the values defined in the functionBasic type variablesAndAll referenced variables of an object are allocated in the function stack memory,When a variable is defined in a code block, Java allocates memory space for the variable in the stack.Scope(For example, if function B is called in function A and variable a is defined in function B, the scope of variable a is only function B. After function B is run, variable a is automatically destroyed. Memory allocated to it will be recycled), Java will automatically release the memory space allocated for this variable, and this memory space can be used for other purposes immediately.

  Heap memory is used to store memory arrays created by newThe memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector.After an array or object is generated in the heap, you can define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, the variable in the stack becomes the referenced variable of the array or object. Later, you can use the variables in the stack in the program to access the array or object in the stack, the referenced variable is equivalent to an array or object name.The referenced variable is a common variable. It is assigned to the stack during definition. The referenced variable is released after the program runs beyond other scopes. Arrays and objects are allocated in the heap in this province. Even if the program runs beyond the code block where the new or object statements are generated, the memory occupied by arrays and objects in this province will not be released. Arrays and objects become junk only when no referenced variable points to it, and cannot be used any more. They are removed (released) by the garbage collector at an uncertain time ). This is also the reason why Java accounts for memory. In fact, the variables in the stack point to the variables in the heap memory,This is the pointer in Java.

Code instance Demo1: creating a single object

1 class Person {2 String name; 3 int age; 4 public void tell () {5 System. out. println ("name:" + name + ", age:" + age); 6} 7} 8 9 public class Demo1 {10 public static void main (String [] args) {11 Person per = new Person (); 12} 13}

An object per is instantiated in the above program. During the instantiation process, space needs to be opened up in the memory, which includes the stack memory and heap memory. The specific memory allocation is shown in:

 

Figure 1-1 Object Instantiation Process

We can find that the object name per is saved in the stack memory (More accurately, the stack memory stores the access address of the heap memory space.), And the specific content of the object, such as the attribute name and age, is stored in the heap memory. Because the per object is only instantiated and has not been assigned a specific value, it is the default value. The default value of the string is null, and the default value of the int type is 0. As mentioned above, the new keyword must be used to open up heap memory space.

Code instance Demo2: Multiple object Creation

1 class Person {
2 String name; 3 int age; 4 public void tell () {5 System. out. println ("name:" + name + ", age:" + age); 6} 7} 8 9 public class Demo2 {10 public static void main (String [] args) {11 Person per1 = new Person (); 12 Person per2 = new Person (); 13 per1.name = "Zhang San"; 14 per1.age = 30; 15 per2.age = 33; 17 per1.tell (); 18 per2.tell (); 19 20} 21}

Figure 1-2 instantiate two objects

Key Concept: A class, like an array, belongs to the reference type. The reference type means that the same heap memory can be pointed to by multiple stack memories. Let's take a look at the simple instance passed by the reference.

Code instance Demo3: Object Reference transfer 1

1 class Person {2 String name; 3 int age; 4 public void tell () {5 System. out. println ("name:" + name + ", age:" + age); 6} 7} 8 9 public class Demo3 {10 public static void main (String [] args) {11 Person per1 = new Person (); 12 Person per2 = per1; // ------- note -------- 13 per1.name = "Zhang San"; 14 per1.age = 30; 15 per2.age = 33; 16 per1.tell (); 17 per2.tell (); 18 19} 20}

The program running result is:

From the running results of the program, we can find that,The output content of the two objects is the same. In fact, the so-called reference transfer means that the right to use a heap memory space is handed over to multiple stack memory spaces. The content of heap memory space can be modified for each stack memory space.The memory distribution diagram of this program is as follows:

Figure 1-3 Transfer memory allocation referenced by an object

 

Figure 1-3 Transfer memory allocation referenced by an object (continued)

Note:In the above example, object per2 does not have heap memory space, because object per2 only declares and does not instantiate. The heap memory space will only be available after instantiation using the new keyword.

Code instance Demo4: Object Reference transfer 2

1 class Person {2 String name; 3 int age; 4 public void tell () {5 System. out. println ("name:" + name + ", age:" + age); 6} 7} 8 9 public class Demo4 {10 public static void main (String [] args) {11 Person per1 = new Person (); 12 Person per2 = new Person (); 13 per1.name = "Zhang San"; 14 per1.age = 30; 15 per2.name = "Li Si "; 16 per2.age = 33; 17 18 per2 = per1; // ----- note ---- 19 20 per1.tell (); 21 per2.tell (); 22 23} 24}

The running result is:

The output result of the program is similar to Demo3. However, the memory allocation has changed as follows:

Figure 1-4 (SPAM object) generation

Note:

1. java itself provides a Garbage Collection mechanism (Garbage Collection, GC), which occasionally releases unused memory space. As long as the object is no longer used, it will wait for the GC to release the space, for example, name = "Li Si"; age = 33 in the heap memory.

2. A stack memory can only point to one heap memory space. If you want to point to other heap memory space, you must first disconnect the existing point to allocate a new point.

Common memory areas in Java

There are mainly four Memory Spaces in Java. These memory names and functions are as follows:

1. stack memory space: Save the names of all objects.

2. heap memory space: stores the specific attributes of each object.

3. Global Data zone: stores static property values.

  4. Global Code area: Save all method definitions.

Finished...

"Success lies in persistence"

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.