简体   繁体   中英

memory allocation , stack and heap

when are the functions put on the stack ?

like , when they are compiled , each function is put on the stack , and then when an object calls it , it goes to that function on the stack ?

OR

first the main function is loaded onto stack , then objects are allocated in heap , and then as the objects call the function they are put onto the stack , each time allocating memory to the local vars on the stack ?

and do static methods also follow the same procedure ,like the normal functions , cause i heard they are allocated memory only once.

so help me to identify which of the pictorial representation for the program is correct...

在此处输入图片说明

在此处输入图片说明

Functions are not put on the stack. The call stack contains call frames and when a function is called a new call frame is pushed. It is popped when the function returns.

A call frame generally also contains some reference or pointer to the [calling] function's code (ie a return address) usually represented as java bytecode

This is a very common scenario, but not a universal one. See old A.Appel's Compiling with Continuations book (describing an SML/NJ implementation without any stack).

when are the functions put on the stack ?

The stack contains data, not functions. It wouldn't make sense to pre allocate space for a function as it can be called recursively, or not at all.

when they are compiled , each function is put on the stack ,

Where did you read this?

and then when an object calls it ,

Threads run code, not objects.

it goes to that function on the stack ?

Nothing like it.

first the main function is loaded onto stack ,

functions are not loaded on the stack.

then objects are allocated in heap ,

probably, though they can be allocated on the stack with escape analysis.

and then as the objects call the function they are put onto the stack ,

Threads call methods and when a method is called it can optionally allocate a stack frame. That stack frame can contain local variables and stack assigned objects.

each time allocating memory to the local vars on the stack ?

Each method call can allocate a single block of memory for all local variables which use memory. Note: some local variables can be assigned to registers and might not use stack.

and do static methods also follow the same procedure ,

There is no real difference between static and non-static methods except non-static methods will have an additional argument at the start for the instance.

like the normal functions , cause i heard they are allocated memory only once.

methods only allocate once per call. Say you have a loop with a local variable inside it, it will still only be allocated once.

The stack is something that is build at runtime. If a method (regardless if static or not) is called it's call is put on the stack. If the method finishes it gets removed from the stack, so that the caller is the first element again and continues it's execution.

You can use a debugger to observe this. A debugger shows you the call stack. If you set a breakpoint in your main method and step into another method call, you'll see that that method call is the top element in the stack above the main method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM