In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is java stack and heap". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When all java programs are running, they have to open up a space in memory, and when JVM (java virtual machine) is running, it will be divided into more details. Each piece of memory in the virtual machine is handled in a different way, so it needs to be managed separately.
There are five types of memory in JVM:
Register, local method area, method area, heap memory, stack memory
Today we focus on heap memory and stack memory.
Stack memory:
Stack memory mainly stores local variables, that is, those defined in the method are called local variables. The memory model of the method is called when the thread executes. When the method is executed, a memory structure called "stack frame" is created, and the stack frame stores the local variables used in the method. Operand stack, return address, dynamic connection and other information. Here, the novice mainly understands the local variable table and the return address. As the name implies, the local variable table is used to store local variables, and the method return address means that the method may exit normally or abnormally during operation. However, no matter which exit method, you should let the caller know the location of the method exit, and the method return address is used to determine the exit location. A method is the whole process of a stack frame in the java stack from the time the method is called to the end of the method execution.
Heap memory:
Heap memory stores objects, that is, new, all in the heap, entities are stored in the heap, entities are used to encapsulate data, and encapsulate multiple (multiple attributes of the entity). If a data disappears, the entity does not disappear and can be used, so the heap will not be released at any time, but the stack is different, the stack is stored with a single variable, variables are released, then there is no more. The entities in the heap will not be released, but they will be treated as garbage, and Java has a garbage collection mechanism to collect them from time to time.
Generally speaking, the difference between heap and stack: heap is mainly used to store objects, and stack is mainly used to execute programs.
So what's the difference between stack and heap? we can look at their differences in several ways!
Order:
Stack is first in, first out, and heap is first in, first out.
Cache:
The stack uses a first-level cache, and they are usually in the storage space when they are called, and are released immediately after the call.
The heap is stored in a secondary cache, and the life cycle is determined by the garbage collection algorithm of the virtual machine (it is not reclaimed once it is orphaned). So the speed of calling these objects is relatively slow.
Memory Recycling:
As soon as the variable life cycle of stack memory is over, it will be released, and the entities stored in heap memory will be collected irregularly by the garbage collection mechanism.
Application efficiency:
Stack: allocated by the system itself, the speed is fast, but the programmer can not master it.
Heap: there are generally two ways to request memory, new and malloc,new are used with operators and malloc is a function. The memory applied by programmers is generally slow, but it is easy to produce memory fragments, but it is more convenient to use, has a large amount of space, and is relatively flexible.
This is the end of "what is the java Stack and Heap". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.