5

I recently had an interview and was asked to design/implement stacktrace functionality. This is what I had come up with.

  • Maintain a stack that holds all the method calls from the main point of entrance of the program.
  • If there is an error at any point during the execution, halt the program and print the entire stack by popping every element.

I was then asked two questions:

  1. How/where would this stack be initialized?
  2. How would you decide how much data the stack should store without it running OOM? Why doesn't the JVM ever run OOM cause of the stack?

For the first question I said, the stack should be a static and should be initialized at the start of the program. But I was not sure about the second question. I tried to read how the JVM does this but it was a bit complex. I tried googling for basic implementations but couldn't find any. Would very much appreciate it if someone would just point me to the right direction as to what exactly I should be looking for to answer this.

  • possible duplicate of [Java default stack size](http://stackoverflow.com/questions/20030120/java-default-stack-size) – ControlAltDel Apr 06 '15 at 16:55
  • 1
    Look at Throwables printStackTrace() – user489041 Apr 06 '15 at 16:56
  • Sorry voted to close but I realize these two questions are not the same. You can find a lot of information on stack allocation with this question: http://stackoverflow.com/questions/20030120/java-default-stack-size – ControlAltDel Apr 06 '15 at 16:56
  • If you need the stack trace at some point the simplest way is to throw and catch an exception there and take the stack trace from the exception. – Marius Apr 07 '15 at 08:08
  • @user489041 I think that doesn't give you the information OP is looking for. `printStackTrace()` simply uses `getStackTrace()` and formats it. – m0skit0 Apr 07 '15 at 09:04
  • @Marius That's not the question being asked. – m0skit0 Apr 07 '15 at 09:05

3 Answers3

1

A bit of a open ended question, here's my take:

  1. Stack should not be static - there's one stack per thread, not per program, and threads can be added and removed during the program's lifetime. So the stacks have to be allocated dynamically as well.
  2. a Java stack can overflow. Which may be different from OOM, but not that different. As for what to store in the stack - I'd go with user-configurable, as the requirements are very different when running in development vs. production mode. You could also discuss possible stack management improvements, such as tail-call optimizations. These would prevent stack overflows, but affect that way the code is written.

Anyway, my 2¢.

Michael Bar-Sinai
  • 2,729
  • 20
  • 27
0

I wouldn't store any stack trace information anywhere since this is already stored in the stack frame of each thread. When the stack trace is required (e.g. exception is thrown), I would build it from the stack frame.

Problem is I'm not sure if the stack frame has all the information needed to do so.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

Call Thread.currentThread().getStackTrace() to return an StackElement[] which can be printed to your log.

bond
  • 1,054
  • 8
  • 15