Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement stack trace when there is a failure?

Tags:

java

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.

like image 602
overflowProblem Avatar asked Nov 10 '22 16:11

overflowProblem


1 Answers

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¢.

like image 100
Michael Bar-Sinai Avatar answered Nov 14 '22 21:11

Michael Bar-Sinai