In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces java heap memory and stack memory knowledge points what the relevant knowledge, detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that everyone read this java heap memory and stack memory knowledge points what articles will be harvested, let's take a look at it.
I. Overview
In Java, memory is divided into two types, one is stack memory, and the other is heap memory.
II. Heap memory
1. What is heap memory?
Heap memory is a kind of Java memory, its role is to store Java objects and arrays, when we create a new object or an array, it will open up a space in heap memory for it, for storage.
2. What are the characteristics of heap memory?
The first point: the heap can actually be seen as a pipeline, or it is usually the case of queuing to buy tickets, so the characteristics of the heap memory are: first in, first out, last in, that is, you queue first, okay, you buy tickets first.
The second point is that the heap can dynamically allocate memory size, and the lifetime does not have to tell the compiler in advance, because it allocates memory dynamically at run time, but the disadvantage is that it is slower to access because it has to allocate memory dynamically at run time.
How are new objects allocated in the heap?
Managed by the Java Virtual Machine's automatic garbage collector.
III. Stack memory
1. What is stack memory
Stack memory is another kind of memory in Java, mainly used to execute programs, such as variables of basic types and reference variables of objects.
2. Characteristics of stack memory
The first point: Stack memory is like a mineral water bottle, like putting things in it, then the first to sink into the bottom, so its characteristics are: first in, last out, last in, first out.
The second point: access speed is faster than the stack, second only to the register, stack data can be shared, but the disadvantage is that the size and lifetime of the data in the stack must be determined, lack of flexibility
3. Stack memory allocation mechanism
Stack memory can be referred to as a level 1 cache and is automatically collected by the garbage collector
4. Data sharing
Examples:
int a = 3;int b = 3;
Step 1:
1. int a = 3;
2. Create a reference to variable a
3. Find out if there is a value of 3 in the stack
4. Not found, save 3, a points to 3
Step 2:
1. Treatment b=3
2. Create a reference to variable b
3. Find, assign directly
Third change:
next
a = 4;
Same as above
The value of a changes, a points to 4, and the value of b does not change.
PS: If there are two objects, it is different. The objects point to the same reference. If one changes, the other will change.
4. The difference between stack and heap
The JVM is a stack-based virtual machine, allocating a stack for each newly created thread. In other words, for a Java program, its execution is done by operating on the stack. The stack holds thread state in frames. The JVM performs only two operations on the stack: frame pushing and pop operations.
Difference points:
Heap memory is used to store objects and arrays created by new.
2. Stack memory is used to store methods or local variables, etc.
3. Stack is first-in, first-out, last-in, last-out.
4. Stack is last-in-first-out, first-in-last-out.
Same points:
1. All of them belong to Java memory.
2. The system automatically recycles it, but for heap memory developers typically recycle it.
About "java heap memory and stack memory knowledge points what" the content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "Java heap memory and stack memory". If you still want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.