Something you may not know about java (2) -- heap and stack, java --

Source: Internet
Author: User

Something you may not know about java (2) -- heap and stack, java --

You may already know about the heap and stack when learning and using java, but you may not fully understand them. Today, we will learn the features of stacks and their differences. After understanding this, you may have a deeper understanding of java.

Java Heap Memory)

The Java runtime in the heap mainly recycles objects and JRE classes. When we create an object (that is, a new object), a space is allocated to the object in the heap memory, and the Java garbage collector (GC) runs in the heap memory, once it is found that this object is not referenced anywhere, the memory occupied by this object will be released.
The created object exists in heap memory and has global permissions. It can be referenced anywhere in the application.

Java Stack Memory (Stack Memory)

The heap memory is executed by a single thread and has less space than the heap memory. It includes local variables defined in the method that exist for a short time. These local variables are always referenced to the object in heap memory. Heap memory also features the following features: first-in-first-out. After a method is called, the stack memory generates a memory area for the method to save the basic data type data in the method and reference the local variables of objects in the heap memory. After this method is executed, the block will be recycled by the garbage collector and the next method will continue.

Deep understanding

Let's take a look at a simple piece of code.

 1 public class TestMemory { 2  3     public static void main(String[] args) { 4         int i = 666;//line1 5         Object object = new Object();//line2 6         TestMemory testMemory = new TestMemory();//line3 7         testMemory.test(object);//line4 8     } 9 //line510     private void test(Object params){//line611         String str = params.toString();//line712         System.out.println(str);//line813     }14 }15 //line9

 

The following figure shows the memory information of the above Code.

1. When the program is running, JVM loads the class information at runtime and the class information we write. The class information is loaded and stored in the heap memory.

2. Go to the main method. The first line of line1 creates a basic data type I = 666, so it creates a memory block of the main method, it also contains I = 666;

3. Next, the location of line2 and line3
Object object = new Object ();
TestMemory testMemory = new TestMemory (); creates two local variables, object and testMemory, and stores two new objects, Object and TestMemory, In the heap. (Each time we create a new object, a memory area will be generated in the heap area to allocate space to it ). Both the object and testMemory reference the new object.

4. Now the line4 and test methods are available. When the test method is called, a memory zone (We have said that the stack feature is post-import, first-out), Because the value is passed in Java, a new reference is generated for the parameter object.

5. Finally, toString is called to generate a String constant stored in the String pool, and str references it. (If you are interested in the String class, you may like the "Java" series, which you may not know, the javasstring class.

6. When the test method ends, the variables in it will be recycled by GC. Then the main method ends. Java releases the memory at runtime and ends the running of the program.

Summary

1. When an object is created, it will always allocate space for it in the heap memory and store it. If there is a value assignment, the variable will be referenced to the object. Stack memory is the local variable that stores these referenced objects.

2. The stack memory has a short life cycle, and the heap memory only needs to work from the beginning to the end of program execution. Because the stack memory does not have to execute a method, the memory zone they occupy will be released. Objects in heap memory can be released only when no variable is referenced.

3. The stack memory size is very small than the heap memory size. However, it is faster than the heap memory.

 

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.