Java Memory mechanism

Source: Internet
Author: User

Java memory Mechanism:

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.

areas of memory commonly used in Java

there are mainly 4 memory spaces in Java, the names and functions of these memory are as follows:
1. Stack memory space: Save all object names (more precisely the address of the heap memory space where the reference is saved)
2. Heap memory space: Saves the specific property content of each object.
3. Global Data area: Saves properties of the static type.
4. Global code Area: Save all method definitions.

Memory allocation policy

According to the idea of compiling principle, the memory allocation of program running, there are three kinds of strategies, namely static , heap and stack .
static storage allocation refers to the need to determine the storage space of each data target at run time at compile time, so that it allocates a fixed amount of memory space at compile time. This allocation requires that there should be no mutable data structures (such as mutable arrays) in the program code, and that nested or recursive structures are not allowed, because they can cause the compile-time compiler to not accurately calculate the required storage space size.
A stack storage allocation can also be a dynamic storage allocation, implemented by a stack-like run stack. In contrast to static storage allocations, in a stack-allocation scenario, the program's need for storage space is unknown at compile time and can only be known at run time. However, when running into a program module, it is necessary to know the size of the data area required by the program module in order to allocate memory for it. Consistent with our daily understanding of the stack, the stack storage allocation is distributed according to the principle of advanced post-out.
Heap Storage Allocations are specifically responsible for the allocation of data structures, such as variable-length strings and object instances, that cannot be determined at compile time or at runtime at the entrance of the program module. Heap memory consists of large chunks of available or free blocks, and the memory in the heap can be allocated and disposed in any order.

Java Memory mechanism

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.