Differences between heap and Stack in Java

Source: Internet
Author: User

Some of the basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function.

When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another.

Heap memory is used to hold objects and arrays created by new.

The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine.

After generating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object.

A reference variable is a name that is an array or an object, and you can use reference variables from the stack to access the arrays or objects in the heap later in your program.

The allocation of variables in memory in Java

1. Class variable (static modified variable): When the program loads, the system opens up memory in the heap, and the memory address in the heap is stored on the stack for high-speed access. The life cycle of a static variable--continues until the entire system shuts down

2, instance variables: When you use the Java keyword New, the system in the heap is not necessarily a continuous space allocated to variables (such as class instances), and then based on the scattered heap memory address, the hash algorithm is converted to a long series of numbers to characterize the variable in the heap "physical location." Life cycle of instance variables-when a reference to an instance variable is lost, it is included in the recyclable "list" by the GC (garbage collector), but the memory in the heap is not released immediately

3, local variables: local variables, by declaring in a method, or a code snippet (such as a For loop), execution to it in the stack to open up memory, when the local variable one but out of scope, memory immediately released

Attached: the memory mechanism of Java

Java divides memory into two types: one is stack memory and the other is heap memory.

Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function, and when a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable. The memory space can be used immediately by another.

Heap memory is used to store the objects and arrays created by new, and the memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. The reference variable in the stack can then be used in the program to access an array or an object in the heap, which is equivalent to a name that is an array or an object. A reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed when the program runs beyond the block of code that uses new to produce the array or object's statement, and the array and object become garbage when no reference variable points to it, not in use, but still occupy memory space. The garbage collector takes off (releases) at a later indeterminate time.

This is the reason why Java compares memory, in fact, the variables in the stack point to the variables in the heap memory, this is the pointer in Java!

Differences between heap and Stack in Java

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.