Stack
https://en.wikipedia.org/wiki/Call_stack
The primary purpose of a call stack is to store the return addresses. When a subroutine is called, the location (address) of the instruction at which the calling routine can later resume needs to be saved somewhere. Using a stack to save the return address has important advantages over alternative calling conventions. One is that each task can have its own stack, and thus the subroutine can be thread-safe. Another benefit is that by providing reentrancy), recursion) is automatically supported. When a function calls itself recursively, a return address needs to be stored for each activation of the function so that it can later be used to return from the function activation. Stack structures provide this capability automatically.
a call stack may serve additional purposes, including for example: Local data storage, Parameter passing, Evaluation stack, Pointer) to current instance, Enclosing subroutine context and Other return state (exception, etc).
Stack Structure
A call stack is composed of stack frames (also called activation records or activation frames). Each stack frame corresponds to a call to a subroutine which has not yet terminated with a return. 通常包含一下几个components:
- the arguments (parameter values) passed to the routine (if any);
- the return address back to the routine’s caller (e.g. in the running method’s stack frame, an address into the calling method’s code); and
- space for the local variables of the routine (if any).
每个stack都有一个stack pointer指向它的最顶部. Popping a frame off the stack does not constitute a fixed decrement of the stack pointer. At function return, the stack pointer is instead restored to the frame pointer, the value of the stack pointer just before the function was called.
Tail call
https://en.wikipedia.org/wiki/Tail_call
A tail call is a subroutine call performed as the final action of a procedure. Tail calls can be implemented without adding a new stack frame to the call stack. Most of the frame of the current procedure is no longer needed, and can be replaced by the frame of the tail call, modified as appropriate. (打比方就像带了parameter的GOTO, can be uniformly coded as [machine code] JUMP instructions)
1 |
function foo1(data) { |
the call to a(data) is in tail position in foo2, but it is not in tail position either in foo1 or in foo3, because control must return to the caller to allow it to inspect or modify the return value before returning it.
Stack vs Heap
一个关于Mem Magt的好链接, 讲了GC, 还有stack vs heap
https://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java
1. Difference between stack and heap memory in Java
http://javarevisited.blogspot.com.au/2013/01/difference-between-stack-and-heap-java.htmlhttps://www.journaldev.com/4098/java-heap-space-vs-stack-memory
Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method.
The stack is attached to a thread, so when the thread exits the stack is reclaimed. The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.
The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor’s cache, making it very fast. Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be - typically - synchronized with “all” other heap accesses in the program.
来自 http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap
JVMs can use a technique called escape analysis, by which they can tell that certain objects remain confined to a single thread for their entire lifetime, and that lifetime is bounded by the lifetime of a given stack frame. Such objects can be safely allocated on the stack instead of the heap. Even better, for small objects, the JVM can optimize away the allocation entirely and simply hoist the object’s fields into registers. Thus, beyond saying “the object is created and the field is there too”, one cannot say if something is on the heap or on the stack. Note that for small, short lived objects, its possible that the ‘object’ won’t exist in memory as such and may instead have its fields placed directly in registers. JVMs are surprisingly good at figuring out things that we used to assume only the developer could know. By letting the JVM choose between stack allocation and heap allocation on a case-by-case basis, we can get the performance benefits of stack allocation without making the programmer agonize over whether to allocate on the stack or on the heap. Thus, if you have code that looks like:
1 |
void foo(int arg) { |
where the … doesn’t allow qux to leave that scope, qux may be allocated on the stack instead. This is actually a win for the VM because it means it doesn’t need to ever be garbage collected - it will disappear when it leaves the scope.
http://programmers.stackexchange.com/questions/65281/stack-and-heap-memory-in-java
2. Java Heap Space or Java Heap Memory
http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html
Heap in Java generally located at bottom of address space and move upwards. whenever we create object using new operator or by any another means object is allocated memory from Heap and When object dies or garbage collected ,memory goes back to Heap space in Javahttp://programmers.stackexchange.com/questions/65281/stack-and-heap-memory-in-java




近期评论