Java heap memory and stack memory similarities and differences (Java heap memories vs stack memories Difference)

Source: Internet
Author: User

--reference Java Heap Memory vs Stack Memory Difference

In data structures, heaps and stacks can be said to be the two most basic data structures, and what are the similarities and differences between stack memory space and heap memory space in Java, and what is the relationship to the stack in the data structure?

One, Java heap storage space

Heap memory (heap storage space) is assigned to a class of objects (object) or JRE at Java Runtime. As soon as we create an object, the heap will definitely allocate a chunk of storage space to the object. The Java garbage collection we know is done on heap storage to free objects that don't have any references to themselves. Any object allocated in the heap has global access and can be referenced from anywhere in the application.

Second, Java stack storage space

The Java stack storage space is used for thread execution. The stack space contains special variables such as short life cycles and references to other objects in the heap. The stack storage space here satisfies the order of LIFO. When a function is called, a new storage space is allocated to the stack, which holds the basic data of the function ("Java experience summarizes a" Java basic type and wrapper type resolution) and references to other objects in the function. Once the function execution is finished, the storage space is freed for use by the next function.

Stack storage space is much smaller than heap storage space

Iii. examples

Memory.java

1  Public classMemory {2  3      Public Static voidMain (string[] args) {//Line 14         intI=1;//Line 25Object obj =NewObject ();//Line 36Memory mem =NewMemory ();//Line 47Mem.foo (obj);//Line 58}//Line 99  Ten     Private voidFoo (Object param) {//Line 6 OneString str = param.tostring ();////Line 7 A System.out.println (str); -}//Line 8 -   the}

Shows how stack storage spaces store basic types, objects, and references to variables

Program execution Process:

    1. Once we start running the program, it loads all the runtime classes into the heap storage space. When the program runs to the first line of the main () function, Java runtime allocates stack storage space for the main function thread.
    2. We created the base data type on the second line, so it will be stored in the stack storage space of the main function thread;
    3. Because we created the object in the third line, it is created in the heap, and a reference to it is saved in the stack space. Similarly, line fourth creates a memory object.
    4. When we call the Foo () function in line fifth of Main (), a space is allocated at the top of the stack space for use by the Foo () function. Because Java is a value pass (Java is passed as a value instead of a reference pass), a new reference is created on line sixth of the Foo function that points to the object in the heap
    5. A string is created on line 7th, which is placed in the string pool of heap space (string Poll), and a reference to it is saved in the stack space.
    6. In line 8th, the Foo function is completed and its stack space is freed
    7. In line 9th The main function is finished, and the space allocated to the main function in the stack is freed. The program also executes in this line, so the Java runtime (Java Runtime) frees up all memory space and terminates the execution of the program.
Iv. stack similarities and differences
    1. The heap storage space can be used by any part of the application, while the stack storage space is only used by the corresponding execution thread
    2. Once an object is created, it is allocated a storage space in the heap and a reference to it is kept in the stack space. Only the basic data types and reference variables that point to the objects in the heap are saved in the stack space
    3. The objects stored in the heap can be accessed globally, but the variables stored in the stack cannot be accessed by other threads (functions)
    4. Memory management in the stack takes the advanced back-out (LIFO) approach, but memory management in the heap is more complex because the objects in the heap can be used globally. The storage space is divided into young-generation,old-generation and so on, this I will summarize after
    5. Stack storage is a short life cycle, but heap storage is much longer
    6. Use-XMS and-xmx to indicate the heap initial and maximum space of the JVM, using-XSS to define the stack space size
    7. When the stack space is full, the Java runtime throws Java.lang.StackOverFlowError, but the heap is full and throws a Java.lang.OutOfMemoryError:Java heap space error
    8. Stack space is very small compared to heap space, and because the stack uses the simplest advanced post-out (LIFO) principle, it is much faster than the heap.

Java heap memory and stack memory similarities and differences (Java heap memories vs stack memories Difference)

Related Article

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.