6

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?

codingenious
  • 8,385
  • 12
  • 60
  • 90

3 Answers3

5

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.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 2
    Just wanted to add that this all does not mean that you should cripple your design to have less modules with more code. it is very unlikely that you run out of memory because of modular design – LionC Nov 20 '13 at 11:29
  • 1
    Indeed, in 99% of cases stack overflows occur because of a program attempting infinite recursion – Richard Tingle Nov 20 '13 at 11:34
  • any idea, how much memory a frame takes? – codingenious Nov 20 '13 at 11:36
  • 1
    @Batty Having a bit of a play the maximum depth seems to be 1024 (for me at least), this seems likely to be the problem first if the pure frame memory is the main stack memory – Richard Tingle Nov 20 '13 at 11:45
  • 1
    @Batty It depends. It includes local variables and parameters among other things, so it's not the same for every method call. – Kayaman Nov 20 '13 at 11:59
  • @Kayaman +1 for comment above. But my doubt was regarding empty stack frame actually. – codingenious Nov 20 '13 at 12:04
  • Seems to be very little information about it. In any case it's OS (32/64-bit) and VM dependent. Are you just curious or what's the interest behind this? – Kayaman Nov 20 '13 at 12:07
4

As stated in Inside the Java Virtual Machine,

The stack frame has three parts: local variables, operand stack, and frame data. The sizes of the local variables and operand stack, which are measured in words, depend upon the needs of each individual method. These sizes are determined at compile time and included in the class file data for each method. The size of the frame data is implementation dependent.

When the Java virtual machine invokes a Java method, it checks the class data to determine the number of words required by the method in the local variables and operand stack. It creates a stack frame of the proper size for the method and pushes it onto the Java stack.

Community
  • 1
  • 1
codingenious
  • 8,385
  • 12
  • 60
  • 90
  • thank you for posting this. So this means that the stack frame is created of constant size when a function is called and can't be resized, right? – Dhruv garg Oct 26 '20 at 07:55
2

You can count the number of stack frames on the current thread's stack as:

Thread.currentThread().getStackTrace().length

In your case, getting this value in get2 would give you a larger value than getting it directly in oneWay (larger by 2). The exact values depend on where in your code you call oneWay. Although some other programming languages don't necessarily create a stack frame for every function call, this is always the case for Java. The JVM spec states explicitly in §2.6 that "A new frame is created each time a method is invoked." and later "Frames are allocated from the Java Virtual Machine stack". So in Java every method call sets up a stack frame, and every stack frame takes memory on the stack. Even when invoking a function that does nothing, a certain amount of memory is required by the virtual machine to keep track of the stack frame itself.

GOTO 0
  • 42,323
  • 22
  • 125
  • 158
  • @DaveNewton no, but there are programming languages that don't necessarily set up a stack frame per method call, for example C++. – GOTO 0 Nov 20 '13 at 12:08
  • @DaveNewton I reworded that sentence to address your concerns. – GOTO 0 Nov 20 '13 at 13:28