In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to understand Java static variables and dynamic variables". In daily operation, I believe many people have doubts about how to understand Java static variables and dynamic variables. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to understand Java static variables and dynamic variables". Next, please follow the editor to study!
[d5000@D5000Client temp] $size temp (executable)
Text text segment data data segment bss dec hex filename
2532 596 296 3424 d60 temp
1. Initialized global and static variables are stored in the
In the segment. Data
2. Uninitialized global and static variables are stored in the
BSS segment. (this section is cleared before the read-write program is run)
3. The local variables declared inside the function are saved in the stack segment. Stack heap
4. Const-decorated local variables are stored in the stack segment. Stack heap
Const-decorated global variables are saved in a text segment, text
5. String constants are saved in the text segment. Text
1. Text segment: contains the instructions of the program, which generally do not change during the execution of the program.
2. Data segment: contains initialized global and static variables, as well as their values.
3. BSS section: contains uninitialized global variables and static variables.
4. Stack segment: contains local variables declared inside the function. Of course, the role of the above paragraph is not only that, the specific role
According to the scope of the variable, it is divided into local variable auto register and global variable (static local variable) static extern
The difference between dynamic and static variables:
1. Storage location
Dynamic variables: stored in memory out-of-stack data area
Static variables: stored in the global data area (static data area)
2. Life cycle
Dynamic variables: determined according to the location you define, such as what you define in a function, then variables beyond the scope of the function will be invalidated
Static variables: released at the end of the program
3. Scope
Dynamic variables: again, it can only be determined according to the location you define, as in the second point.
Static variables: valid in the current file
Heap stack (Stack)
1. Memory allocation:
Heap: generally allocated and released by programmers, if programmers do not release, the program may be reclaimed by OS at the end of the program. Note that it is different from the heap in the data structure, and the allocation is similar to the linked list. The keywords you might use are as follows: new, malloc, delete, free, and so on.
Stack: automatically allocated and released by the compiler (Compiler), storing the parameter values of the function, the values of local variables, etc. It operates in a manner similar to the stack in the data structure.
2. Application method:
Heap: programmers are required to apply and specify the size. In c, malloc functions such as p1 = (char *) malloc (10); in C++, use the new operator, but note that p1 and p2 themselves are on the stack. Because they can still be thought of as local variables.
Stack: automatically assigned by the system. For example, declare a local variable int b in the function; the system automatically opens up space for b in the stack.
3. System response:
Heap: the operating system has a linked list of free memory addresses. when the system receives an application from the program, it traverses the list to find the first node whose space is larger than the applied space, and then removes the node from the list of idle nodes and allocates the space of the node to the program. in addition, for most systems, the size of this allocation is recorded at the first address in this memory space. Only in this way can the delete statement in the code release the local memory space correctly. In addition, because the size of the found heap node is not necessarily equal to the size of the application, the system will automatically put the excess part back into the free linked list.
Stack: as long as the remaining space of the stack is larger than the applied space, the system will provide memory for the program, otherwise an exception will be reported that the stack will overflow.
4. Size limit:
Heap: a data structure that extends to a high address and is a discontiguous area of memory. This is because the system uses linked lists to store free memory addresses, which are naturally discontiguous, and the traversal direction of the linked list is from low address to high address. The size of the heap is limited by effective virtual memory in the computer system. Thus it can be seen that the space obtained by the heap is more flexible and larger.
Stack: under Windows, a stack is a data structure extended to a low address, a contiguous area of memory. This sentence means that the address at the top of the stack and the maximum capacity of the stack are predetermined by the system. Under WINDOWS, the size of the stack is fixed (it is a constant determined at compile time). If the applied space exceeds the remaining space of the stack, it will prompt overflow. Therefore, there is less space that can be obtained from the stack.
5. Efficiency:
Heap: is allocated by new memory, generally slow, and easy to produce memory fragments, but the most convenient to use, in addition, in WINDOWS, the best way is to allocate memory with VirtualAlloc, he is not in the heap, nor in the stack is directly in the process address space to retain a fast memory, although the most inconvenient to use. But the speed is fast and the most flexible.
Stack: automatically allocated by the system, the speed is fast. But programmers can't control it.
6. Storage content:
Heap: the size of the heap is usually stored in a byte at the head of the heap. The details in the heap are arranged by the programmer.
Stack: the first to enter the stack during a function call is the address of the next instruction in the main function (the next executable statement of the function call statement) and then the parameters of the function. In most C compilers, the parameters enter the stack from right to left, and then the local variables in the function. Note: static variables are not on the stack. When this function call ends, the local variable goes off the stack first, then the parameters, and finally the pointer at the top of the stack points to the original address, that is, the next instruction in the main function, from which the program continues to run.
7. Access efficiency:
Heap: char * S1 = "Hellow Word"; is determined at compile time; slow
Stack: char S1 [] = "Hellow Word"; is assigned at run time; using an array is faster than using a pointer, because the pointer needs to be transferred in the edx register in the underlying assembly, and the array is read directly on the stack.
two。
The difference between heap and stack (stack and heap)
It is generally believed that it is divided into these storage areas in c
1 stack-there is a compiler automatic allocation release
2 heap-generally allocated and released by the programmer, if the programmer does not release, the program may be reclaimed by OS at the end of the program
3 Global area (static zone), global variables and static variables are stored together, initialized global variables and static variables are in the same area, uninitialized global variables and uninitialized static variables are in another adjacent area. The program ends and is released.
4 there is also a special place to put constants. -Program ends release
The variables defined in the function body are usually on the stack, and the functions that allocate memory such as malloc, calloc, realloc, etc., are allocated on the heap. In all functions outside the definition is the global quantity, after adding the static modifier no matter where are stored in the global area (static zone), in all functions outside the definition of static variables are valid in this file, can not be extern to other files with, in the function body defined static representation is only valid in the function body. In addition, strings such as "adgfdf" in the function are stored in the constant area.
For example:
Int a = 0; global initialization zone
Char * p1; global uninitialized zone
Main ()
{
Int b; stack
Char s [] = "abc"; stack
Char * p2; stack
Char * p3 = "123456"; 123456\ 0 in the constant area, p3 on the stack.
Static int c = 0; global (static) initialization zone
P1 = (char *) malloc (10)
P2 = (char *) malloc (20)
The area with 10 and 20 bytes allocated is in the heap area.
Strcpy (p1, "123456"); 123456\ 0 is placed in the constant area, and the compiler may associate it with the "12345" that p3 points to.
6 "optimized into one piece.
}
In addition, when the function is called, there will be a series of operations on the stack to preserve the site and pass parameters. The space size of the stack is limited, and the default for vc is 2m. The case where the stack is not enough is that the program allocates a large number of arrays and recursive functions are too deep. It is important to know that when a function returns after a call, it frees up all the stack space in the function. The stack is automatically managed by the compiler and you don't have to worry about it. The heap allocates memory dynamically, and you can allocate a lot of memory. But if it is not used properly, it will cause a memory leak. And frequently malloc and free produce memory fragmentation (a bit like disk fragmentation), because c is looking for matching memory when allocating dynamic memory. On the other hand, using the stack will not produce fragments. Accessing data on the stack is faster than accessing data on the heap through pointers.
Generally speaking, stack and stack are the same, that is, stack, but heap is heap when heap is said.
The stack is first-in-first-out, generally growing from a high address to a low address.
Heap (heap) and stack (stack) are two basic concepts inevitably encountered in Cramp Clipper + programming. First of all, both of these concepts can be found in books on data structures, both of which are basic data structures, although the stack is simpler. These two concepts are not parallel in the specific CumberCraft + programming framework. The study of the underlying machine code can reveal that the stack is the data structure provided by the machine system, while the heap is provided by the Cpicard + function library. Specifically, modern computers (serial execution mechanism) support the stack data structure directly at the bottom of the code. This is reflected in that there are special registers pointing to the address of the stack and special machine instructions to complete the operation of data in and out of the stack. This mechanism is characterized by high efficiency, limited data support, generally integers, pointers, floating-point numbers and other data types directly supported by the system, and does not directly support other data structures. Because of this characteristic of the stack, the use of the stack is very frequent in the program. The call to the subroutine is done directly using the stack. The call instruction of the machine implies the operation of pushing the return address onto the stack and then jumping to the subroutine address, while the ret instruction in the subroutine implies the operation of popping up the return address from the stack and jumping it. The automatic variable in CAccord + is an example of using the stack directly, which is why the automatic variable of the function is automatically invalidated when the function returns (because the color change pointer stamps the dark state).
Unlike the stack, the data structure of the heap is not supported by the system (whether machine system or operating system), but provided by the library. The basic malloc/realloc/free function maintains an internal set of heap data structures. When a program uses these functions to obtain new memory space, this set of functions first tries to find available memory space from the internal heap. If there is no available memory space, it attempts to use system calls to dynamically increase the memory size of the program data segment. The newly allocated space is first organized into the internal heap and then returned to the caller in an appropriate form. When the program releases the allocated memory space, it is returned to the internal heap structure and may be properly processed (such as merging with other free space to form a larger free space) to better fit the next memory allocation request. This complex allocation mechanism is actually equivalent to a memory-allocated buffer pool (Cache) for several reasons:
1. System calls may not support memory allocation of any size. The system calls of some systems only support memory requests of fixed size and multiples (allocated by page); this can be a waste for a large number of small memory categories.
two。 System calls to request memory can be expensive. System calls may involve the transformation of the user state and the kernel state of mind.
3. Unmanaged memory allocation can easily cause memory fragmentation under a large number of complex memory allocation and release operations.
Comparison of heap and stack
From the above knowledge, we can see that the stack is the function provided by the system, which is characterized by fast and efficient, limited and inflexible data, while the stack is the function provided by the function library, which is characterized by flexibility and convenience, a wide range of data adaptation, but a certain reduction in efficiency. The stack is the system data structure and is unique to the process / thread; the heap is the internal data structure of the function library and is not necessarily unique. Memory of different heaps cannot operate with each other. Stack space can be divided into static allocation and dynamic allocation. Static allocation is done by the compiler, such as automatic variable (auto) allocation. Dynamic allocation is done by the alloca function. The dynamic allocation of the stack does not need to be released (automatically), so there is no release function. For the sake of portable programs, dynamic allocation of stacks is not encouraged! Heap space allocation is always dynamic, although all data space will be released back to the system at the end of the program, but accurate request memory / free memory matching is an essential element of a good program.
You can think about it together.
The growth direction of stack and stack is just the opposite.
-
So the heap and stack in the computer are often talked about together.
Nod is generally not necessary, do not create dynamically, the most hate to use the new out of things as local variables, with the practice of delete immediately.
Reasons
1. Stack allocation is faster than heap allocation. Only one instruction is needed to assign all local variables.
two。 There is no memory fragmentation in the stack.
three. Stack objects are easy to manage
Of course, it should be written that way in some cases, such as
1. The object is very large.
two。 The object needs to be constructed or analyzed at a particular time.
3. Classes only allow objects to be created dynamically, such as most classes in VCL
Of course, you can't avoid it when you have to use heap objects.
What are the functions of heap memory and stack memory respectively?
Heap: random order
Stack: first in, then out
The difference between a stack and a stack
1. Preliminary knowledge-memory allocation of programs
The memory consumed by a program compiled by cmax Clipper + is divided into the following parts
1, stack area (stack)-automatically allocated and released by the compiler, storing the parameter values of the function, the values of local variables, etc. It operates in a manner similar to the stack in the data structure.
2, heap area (heap)-generally allocated by programmers to release, if programmers do not release, the end of the program may be recycled by OS. Note that it is different from the heap in the data structure, and the way of allocation is similar to the linked list, hehe.
3. Global area (static zone) (static)-the global and static variables are stored together, the initialized global variables and static variables are in the same area, and the uninitialized global variables and uninitialized static variables are in the adjacent area. -the system is released at the end of the program
4. The text constant area-the constant string is put here. Released by the system at the end of the program
5. Program code area-the binary code that stores the body of the function.
2. Example program
It was written by a senior, and it's very detailed.
/ / main.cpp
Int a = 0; global initialization zone
Char * p1; global uninitialized zone
Main ()
{
Int b; stack
Char s [] = "abc"; stack
Char * p2; stack
Char * p3 = "123456"; 123456\ 0 in the constant area, p3 on the stack.
Static int c = 0; global (static) initialization zone
P1 = (char *) malloc (10)
P2 = (char *) malloc (20)
The area with 10 and 20 bytes allocated is in the heap area.
Strcpy (p1, "123456"); 123456\ 0 is placed in the constant area, and the compiler may optimize it in the same place as the "123456" pointed to by p3.
}
Second, the theoretical knowledge of stack and stack
2.1 Application method
Stack: automatically assigned by the system. For example, declare a local variable int b in the function; the system automatically opens up space for b in the stack
Heap: programmers are required to apply and specify the size. In c, malloc functions such as p1 = (char *) malloc (10); in C++, use new operators such as p2 = (char *) malloc (10); but note that p1 and p2 themselves are in the stack.
2.2 response of the system after application
Stack: as long as the remaining space of the stack is larger than the applied space, the system will provide memory for the program, otherwise an exception will be reported that the stack will overflow.
Heap: first of all, you should know that the operating system has a linked list of free memory addresses. when the system receives an application from the program, it will traverse the list to find the first node whose space is larger than the applied space. then delete the node from the list of idle nodes and allocate the space of the node to the program. in addition, for most systems, the size of this allocation will be recorded at the first address in this memory space. In this way, the delete statement in the code can release the local memory space correctly. In addition, since the size of the found heap node is not necessarily equal to the size of the application, the system will automatically put the excess part back into the free linked list.
2.3 limit on the size of the application
Stack: under Windows, a stack is a data structure extended to a low address, a contiguous area of memory. This sentence means that the address at the top of the stack and the maximum capacity of the stack are predetermined by the system. Under WINDOWS, the size of the stack is 2m (some say 1m, anyway, it is a constant determined at compile time). If the applied space exceeds the remaining space of the stack, it will prompt overflow. Therefore, there is less space that can be obtained from the stack.
Heap: a heap is a data structure that extends to a high address and is a discontiguous area of memory. This is because the system uses linked lists to store free memory addresses, which are naturally discontiguous, and the traversal direction of the linked list is from low address to high address. The size of the heap is limited by effective virtual memory in the computer system. Thus it can be seen that the space obtained by the heap is more flexible and larger.
2.4 comparison of application efficiency:
The stack is automatically allocated by the system, and the speed is fast. But programmers can't control it.
Heap is memory allocated by new, which is generally slow and prone to memory fragmentation, but it is the most convenient to use. In addition, under WINDOWS, the best way to allocate memory is to use VirtualAlloc, which is not in the heap, nor in the stack, but directly in the address space of the process, although it is the most inconvenient to use. But it is fast and flexible.
2.5 Storage in heaps and stacks
Stack: in a function call, the first one to enter the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), followed by the parameters of the function, which in most C compilers enter the stack from right to left, followed by local variables in the function. Note that static variables are not on the stack. When this function call ends, the local variable goes off the stack first, then the parameters, and finally the pointer at the top of the stack points to the original address, that is, the next instruction in the main function, from which the program continues to run.
Heap: the size of the heap is usually stored in a byte at the head of the heap. The details in the heap are arranged by the programmer.
2.6 comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaaaa"
Char * S2 = "bbbbbbbbbbbbbbbbb"
Aaaaaaaaaaa is assigned at run time; bbbbbbbbbbb is determined at compile time; however, in later access, the array on the stack is faster than the string pointed to by the pointer, such as the heap.
For example:
# i nclude
Void main ()
{
Char a = 1
Char c [] = "1234567890"
Char * p = "1234567890"
A = c [1]
A = p [1]
Return
}
Corresponding assembly code
A = c [1]
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4], cl
A = p [1]
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]
00401070 8A 42 01 mov al,byte ptr [edx+1]
00401073 88 45 FC mov byte ptr [ebp-4], al
The first reads the elements in the string directly into the register cl, while the second reads the pointer value into edx first, which is obviously slow when reading characters according to edx.
2.7 Summary:
The difference between the stack and the stack can be seen in the following analogy: using the stack is like going to a restaurant to eat, just order food (send an application), pay, and eat (use), and leave when you are full. Don't worry about the preparation work such as cutting and washing vegetables, and cleaning dishes and washing pans. His advantage is that he is fast, but he has little freedom. Using heap is like making your own favorite dishes, which is more troublesome, but it is more in line with your own taste and has a lot of freedom.
The difference between heap and stack is mainly divided into:
The heaps and stacks of the operating system, such as those mentioned above, do not say much. Then there are the heaps and stacks of data structures, which are different concepts. The heap here actually refers to a data structure of a priority queue (which satisfies the nature of the heap), with the first element having the highest priority; the stack is actually a mathematical or data structure that satisfies the first-in-and-out nature. Although the words "stack" and "stack" are called together, they are still very different, only for historical reasons.
At this point, the study on "how to understand Java static variables and dynamic variables" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.