I recently had an interview and was asked to design / implement a stacktrace function. This is what I came up with.
- Maintain a stack containing all method calls from the main entry point into the program.
- If there is an error at run time, stop the program and print the entire stack by clicking on each item.
Then I was asked two questions:
- How / where will this stack be initialized?
- How would you determine how much data a stack should store without using OOM? Why does the JVM never start OOM because of the stack?
For the first question I said, the stack must be static and must be initialized at the beginning 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 complicated. I tried a google search for basic implementations but could not find. I would really appreciate it if someone just pointed me in the right direction as to what exactly I should look for in order to answer this.
source share