Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size taken by stack frame

Java stack create new frame for every method call, But does this frame takes memory on the stack?

To clarify on my question :

public void oneWay()
{
  System.out.println("start");
  get1();
}

private void get1()
{
  System.out.println("get1");
  get2();
}

private void get2()
{
  System.out.println("get2");
}

Output of this is same as :

public void anotherWay()
{
  System.out.println("start");
  System.out.println("get1");
  System.out.println("get2");
}

But does second snippet takes more memory on stack or equal? In short, does stack frame take memory?

EDIT : How much memory does a stack frame take? Is there any specification by Sun, now Oracle?

like image 350
codingenious Avatar asked Nov 20 '13 11:11

codingenious


People also ask

How do you measure stack frame size?

Count the number of complete strings, multiply by 8 (since "STACK---" is 8 bytes long), and you have the number of bytes of remaining stack space.

How many bytes is a stack frame?

The stack frame is 32-byte-aligned.

Is stack frame size fixed?

There is no standard minimum size for a stack frame. The maximum size of a stack frame depends on the platform and implementation. A common implementation is to have the stack to expand towards the heap and the heap expands towards the stack.

What does a stack frame hold?

An individual stack frame has space for actual parameters, temporary locations, local variables and calling subroutine information.


1 Answers

Yes, naturally. That's why you get a stack overflow if you nest too deep. You can use the -Xss command line switch to modify the stack size, if you find that you need to have a bigger (or smaller) stack for your threads.

The specification seems to allow a lot of freedom for the implementation, so all in all, you can't really rely on stack frame size.

like image 58
Kayaman Avatar answered Oct 08 '22 11:10

Kayaman