Home Java JavaBase The difference between heap and stack java

The difference between heap and stack java

Nov 13, 2019 pm 02:32 PM
java the difference stack

The difference between heap and stack java

The difference between heap and stack java

Before talking about heap and stack, let’s talk about JVM (virtual Machine) memory division:

Java programs must allocate space when they are running. Any software must allocate space in the memory when they are running. The Java virtual machine also must allocate space when it is running. The JVM opens up a memory area in the memory when it is running, and divides it more carefully in its own memory area when it starts. Because each piece of memory in the virtual machine is processed in a different way, it must be managed separately.

The JVM memory is divided into five parts:

1. Register;

2. Local method area;

3. Method area;

4. Stack memory;

5. Heap memory.

Let’s focus on the heap and stack:

Stack memory: Stack memory is first of all a memory area, which stores local variables, all defined in methods All are local variables (global variables outside the method). Local variables are also defined inside the for loop. The function must be loaded first before local variables can be defined. Therefore, the method first stacks the stack and then defines the variables. The variables have their own scope. , the variable will be released once it leaves the scope. The stack memory is updated very quickly because the life cycle of local variables is very short.

Heap memory: stores arrays and objects (in fact, arrays are objects). Everything created by new is in the heap. Entities (objects) are stored in the heap. Entities are used to encapsulate data, and It encapsulates multiple (multiple attributes of an entity). If one piece of data disappears, the entity does not disappear and can still be used, so the heap will not be released at any time, but the stack is different. The stack stores only a single variable. Once the variable is released, it is gone. Although the entities in the heap will not be released, they will be treated as garbage. Java has a garbage collection mechanism to collect them from time to time.

Let’s talk about the heap and stack in detail through an illustration:

For example, the statement in the main function int [] arr=new int [3]; in memory How it is defined:

The main function goes to the stack first, defines a variable arr in the stack, and then assigns a value to arr, but the right side is not a specific value, but an entity. The entity is created in the heap. First, a space is opened in the heap through the new keyword. When the memory stores data, it is reflected by the address. The address is a continuous binary, and then a memory address is allocated to the entity. Arrays have an index. After the array entity is generated in the heap memory, each space will be initialized by default (this is a characteristic of heap memory. Uninitialized data cannot be used, but it can be used in the heap. Because it has been initialized but not on the stack), different types have different initialized values. So variables and entities are created in the heap and stack:

The difference between heap and stack java

So how are the heap and stack connected?

We just said that we allocate an address to the heap, assign the address of the heap to arr, and arr points to the array through the address. So when arr wants to manipulate the array, it uses the address instead of assigning entities to it directly. We no longer call this a basic data type, but a reference data type. It is called arr and refers to the entity in the heap memory. (Can be understood as a pointer to c or c. Java grew up from c and is very similar to c, optimizing c)

The difference between heap and stack java

If when int [] arr=null;

arr does not point to anything, and the function of null is to dereference the pointer of the data type.

When an entity is not pointed to by a reference data type, it will not be released in the heap memory, but will be treated as garbage and automatically recycled at an irregular time, because Java has an automatic recycling mechanism. , (while c does not, the programmer needs to manually recycle. If it is not recycled, the heap will grow until it is full and the memory overflows, so Java is better than c in memory management). The automatic recycling mechanism (program) automatically monitors whether there is garbage in the heap. If there is garbage, it will automatically perform garbage collection, but it is not certain when it will be collected.

So the difference between heap and stack is obvious:

1. Stack memory stores local variables while heap memory stores entities;

2. The update speed of stack memory is faster than that of heap memory, because the life cycle of local variables is very short;

3. The variables stored in stack memory will be released once their life cycle ends, while the entities stored in heap memory will be released. It will be collected by the garbage collection mechanism from time to time.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of The difference between heap and stack java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

Concept of c language function Concept of c language function Apr 03, 2025 pm 10:09 PM

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

How to use XPath to search from a specified DOM node in JavaScript? How to use XPath to search from a specified DOM node in JavaScript? Apr 04, 2025 pm 11:15 PM

Detailed explanation of XPath search method under DOM nodes In JavaScript, we often need to find specific nodes from the DOM tree based on XPath expressions. If you need to...

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

The difference in output results of console.log: Why do the same variables have different printing methods but different results? The difference in output results of console.log: Why do the same variables have different printing methods but different results? Apr 04, 2025 am 11:48 AM

In-depth discussion of the differences in console.log output in this article will analyze the reasons why the output results of console.log function in a piece of code are different. Code snippets involve URL parameter resolution...

See all articles