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:
- How/where would this stack be initialized?
- 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.