Detailed java stack memory

Java the memory is divided into two, one called the stack memory, called heap memory

Defined in the function of some basic types of variables and object reference variables are allocated in the stack memory function . When a variable is defined in a code block, java allocated for this variable in the stack memory space, when exceeding the scope of variables, java automatically freed memory space allocated for the variable, the memory space can be immediately for other purposes.

Heap memory used to store objects and arrays created by the new new . In the memory allocated on the heap, java virtual machine managed by automatic garbage collector. After the heap is generated an array or an object may also define a particular variable in the stack, the value of this variable is equal to the first address of an array or an object in the heap memory, this particular variable is in the stack becomes a reference to the variable array or object, since you can use the stack memory reference variables in the program to access the heap array or object reference variables is equivalent to an alias from an array or object, or code.

Reference variables are common variables, the definition of memory allocated on the stack, reference variables to run outside the scope release program. & Object itself and the array allocated on the heap, even outside the program proceeds to use the new sentence is generated and the location of the object code block arrays, arrays and heap memory occupied by the object itself is not released, array and object without reference variable points it is when we come into a garbage can no longer be used, but the memory is still occupied, in a subsequent period of uncertainty is freed garbage collector. This is the main reason java comparison of total memory,In fact, the stack variable to point to heap memory variables, which is a pointer in Java! 


In java compare memory allocation strategy and the heap and stack

  1 memory allocation strategy

  in accordance with the view compiler theory, memory allocation program is running, there are three strategies, namely, static, stack-style, and heap style.

   static storage allocation means can determine storage space requirements for each data object at runtime at compile time, so you can allocate a fixed memory space for them at compile time. this allocation strategy requires application code does not allow variable data structures (such as can be there are variables group), or does not allow nested recursive structure occurs, since they will lead to the compiler can not accurately calculate the storage space requirements.

  stack storage allocation dynamic memory allocation may also be referred to, it is a stack of similar . and static memory allocation opposite runtime stack to achieve, in the stack storage program, the program needs for data area at compile time is completely unknown, only to run when it is able to know, but the law, enter a in operation when the program module, the program must know the module data area size required to be able to allocate memory for. our data structure known Like the stack, the stack storage allocated according to the principle of last-out for distribution.

  Static storage allocation requirements at compile time to know the storage requirements of all variables, stack storage allocation requirements must be aware of all the storage requirements at the entrance process, while the heap storage allocation module is responsible for the entrance at compile time or run time Unable to determine the structure of the memory allocation data storage requirements, such as variable length strings and object instances. heap may utilize a large block or free blocks, the heap memory can be allocated and released in any order.

  Comparative stack and heap 2

  the above summary of the definition comes from the textbook compiler theory, in addition to static memory allocation, it seems so dull and difficult to understand, leaving aside the following static memory allocation, centralized compare heap and stack:

  the function and role of the heap and stack to popular comparison, the heap is mainly used to store objects, the stack is mainly used to execute the program and this in turn was mainly due to the different characteristics of the heap and stack the decision:

  In programming, such as C / C ++, all method calls are carried out by the stack, all local variables, the parameters are in the form of memory allocated from the stack. In fact, not a distribution, but up used on the line from the top of the stack, just like the factory conveyor belt (conveyor belt), like, Stack Pointer will automatically guide you to put it in position, you have to do is put something down on the line. when the exit function, you can modify the contents of the stack pointer stack destruction. such a pattern fastest, of course, used to run the program. it should be noted that, at the time of distribution, such as a soon-to-call program when the module is assigned a data area, you should know in advance the size of this data area, also said that although the allocation is carried out in the program is running, but the size distribution is determined by how much, unchanging, and the "how much size" is determined at compile time, not at runtime.

  heap when the application is running in the operating system's request to allocate their own memory, because the memory allocation from the operating system administration, so in the allocation and destruction must take up time, so use the heap efficiency is very low, but the advantage is that the stack, the compiler does not have to know how much storage space is allocated from the heap, you do not know the data storage must stay on the heap long Time, therefore, will get greater flexibility when using storage on the heap. In fact, object-oriented polymorphism, heap memory allocation is essential, because the multi-state variable storage space required only at run time can be determined after the object is created in C ++, it is required to create an object, only required new commands related to the preparation of the code. When that code is executed, it will automatically save the data in a heap, of course, to achieve this flexibility will inevitably pay a price:! Heap when allocating storage space will spend a longer period of time which is the we just said leads to low efficiency reasons, it appears that Comrade Lenin that good, people are often the advantages of human shortcomings, shortcomings often the person who benefits (halo ~).

  the 3 JVM heap and stack

  JVM is each newly created thread is assigned a stack-based virtual machine .JVM stack is. that is, for a Java program, it is run through the operation of the stack to complete. Stack units of frames saved state of the thread. JVM stacks of only two operations: a frame unit of the push and pop operations.

  We know that the method is called a thread is executing the current method of this thread. We may not know, frame called the current method used in the current frame. When the thread activate a Java method, JVM will be pushed into a new frame in the thread's Java stack inside. The frame will naturally become the current frame. During the execution of this method, this frame will be used to save the parameters, local variables, intermediate calculations and other data. Here the concept and principles of activity compiled a record of this frame is the same.

  From Java point of view of this distribution mechanism, the stack can be understood: a stack (stack) is an operating system in the establishment of a process or thread (thread-support multithreaded operating system) storage area created for this thread, this region has a characteristic, last-out.

  Each Java applications unique to a JVM instances, each instance is unique to a heap. All class instance or an array of applications in the operation are created on the heap by all application threads share. With C / C ++ is different in Java heap memory is allocated automatically initialized. Java, storage space for all objects are allocated on the heap, but the reference object is allocated on the stack, that is allocated memory from two places in the establishment of an object, the actual memory allocated on the heap the establishment of this object, allocated on the stack memory heap is just a pointer to the object (reference) only.

  Java heap and stack in

  Java the memory is divided into two: One is the stack memory, one is the heap memory.

  Variables refer to variables and objects of the type defined in the basic functions are allocated to the stack memory function.

  When defining a variable block of code, Java stack is allocated in variable memory space, when exceeding the scope of variables, Java automatically freed memory space allocated for the variable, the memory space can be immediately for other purposes.

  Heap memory used to store objects and arrays created by the new.

  In the memory allocated on the heap, managed by automatic garbage collector Java virtual machine.

  After the heap produce an array or object, you can also define a special variable in the stack, the stack so that the value of this variable is equal to the first address of the array or object in the heap memory, stack variable became an array or object reference variables.

  Reference variable is equivalent to a name from the array or object, since you can access the heap array or object reference variables using the stack in the program.

  Specifically:

  stack and heap is where Java is used to store data in the Ram. Unlike C ++, Java automatically manage the stack and heap, the programmer can not directly set the stack or heap.

  A Java heap is the runtime data area, the class (object space dispense these objects by establishing new, newarray, anewarray multianewarray and other instructions, program code, they do not need to explicitly release. Heap garbage collection is responsible for the the heap is the advantage of dynamically allocated memory size, survival do not have to tell the compiler in advance, because the dynamic allocation of memory, Java's garbage collector automatically taken away these data it is no longer used at run time. but the disadvantage is that, due to the dynamic allocation of memory access slower at runtime.

  advantage of the stack is accessed faster than reactor, after register, stack data can be shared, but the disadvantage is that there is data in the stack size and survival must be determined, lack of flexibility. the main stack to store some basic types of variables (, int, short, long, byte, float, double, boolean, char) and object handle.

  stack has a very important particularity, the data is present in the stack may simultaneously share Suppose we define:.

  int. 3 = a;

  int. 3 = B;

  The compiler first deal with int a = 3; first it will create a variable as a reference in the stack, and then find out whether there is value 3 stack, if not found, it will come in store 3, then a 3 point. Followed by treatment int b = 3; b After creating the reference variable, as it has the value 3 in the stack, directly to put b 3. Thus, there have been cases a and b simultaneously point to 3. At this time, if we make a = 4; then the compiler will search again if there are 4 value stack, and if not, then the 4 stored in, and make a point to 4; If you already have, then this address directly to a point . Thus a change does not affect the value of the value b. This sharing is to be noted with reference to two simultaneous data objects to an object such sharing is different, because such a situation does not affect the modification B, it is done by the compiler, it facilitates save space. And an object reference variables modify the internal state of this object will affect another object reference variables 

Published 40 original articles · won praise 19 · views 60000 +

Guess you like

Origin blog.csdn.net/u010090644/article/details/49075143