Difference Between Stack and heap memory in Java

Source: Internet
Author: User

I encountered this problem during the interview today. The difference between stack and stack in Java and which operation is more efficient made me difficult ....

Java divides memory into two types: stack memory and stack memory. Stack is used to store objects, and stack is used to run programs.I. Stack
1. Some Defined FunctionsBasic type variablesAndObject reference variableAll are allocated in the function stack memory. For example:
String STR = new string ("test ");
The execution process of this statement is to first create a String object with the value of "test" on the stack, and then create another Str on the stack to point to the newly created "test" object, that is, STR is a reference of "test. Therefore, in Java, object creation is performed on the stack, and object references are generally performed on the stack. Generally, each method call has an independent stack to store reference variables of objects such as Str. After the method returns, the stack is cleared, so the referenced variables are cleared, this is the object on the stack. If no other referenced variable references it, it will be GC at a proper time.
2. When a variable is defined in a code block, Java allocates memory space for the variable in the stack. When the variable's scope is exceeded, java will automatically release the memory space allocated for the variable, and the memory space can be used for another use immediately.2. Heap
Heap memory is used to store objects and arrays created by new. The 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. The referenced variable is equivalent to an array or an object name. Later, you can use the referenced variable in the stack in the program to access the array or object in the heap.
Iii. execution efficiency
The advantage of heap is that the memory size can be automatically allocated, and the editor does not need to be informed about the lifetime. The Java Garbage Collector automatically recycles unnecessary data. The disadvantage is that the storage efficiency is slow due to the need to dynamically allocate memory.
Stack has the advantage of fast access efficiency, second only to registers,Stack data can be shared. However, the disadvantage is that the data size and Storage Period in the stack are fixed and there is no flexibility.
The stack mainly stores some basic data such as int, short, and byte. Stack storage is faster, but deletion is not that good.

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.