2

I am writing an educational program for students, where they can see how different algorithms work when solving Traveling salesman problem (Time consumption, visual representation of state etc.). The problem is that not only I have to show good algorithms but I have to show bad ones as well. For instance I implement Breadth-first search algorithm for TSP (horrible choice).

Program itself is written in Java. I have a seperate thread for problem solver algorithm and all algorithms implement a specific interface which allows me to interfere after each iteration.

All blind-search algoritms which run over a tree structure with N nodes (n= number of cities) and each node is an array of N elements, that implementation produces a StackOverFlow Exception after around generating ~ 50k nodes. I dont want to limit user interface so that limited ammount of cities can be used- simulated anneal works with thousands of cities.

Here is the question: Is there some reliable function I could use in some specified logical statement so that I could identify the point when System is about to crash? Something is style : if (System.memoryLeft() <= 100 /bytes/ ) { // Stop working and take action

Thanks in advance.

Erich Jagomägis
  • 285
  • 1
  • 4
  • 13

2 Answers2

2

The simplest way to detect it will throw an Error is to wait until it throws and error and catch it.

try {
   action();
} catch(StackOverflowError ste) {
   // you can't call anything here safely, but you can return or unwind the stack.
}

Note: the maximum stack size varies between machine and is based on command lines settings. It is not based on a number of calls.

If you are really concerned about this issue, I suggest changing the code do it doesn't use recursion and you avoid this issue completely.


The problem is that the maximum stack depth is displayed/recorded is 1024. If you have a longer stack, you won't see what caused it originally. What you can do is to reduce the maximum stack size with -Xss128k (or less if your JVM allows it) so that your stack traces will always be short enough to be captured.

public static void main(String... ignored) {
    callMe(1);
}

private static void callMe(int i) {
    callMe(i);
}

when called with a smaller stack size

at Main.callMe(Main.java:42)
at Main.callMe(Main.java:42)
at Main.callMe(Main.java:42)

many deleted

at Main.callMe(Main.java:42)
at Main.callMe(Main.java:42)
at Main.callMe(Main.java:42)
at Main.main(Main.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • They're asking how to check when the program is about to crash, not how to display the full stack trace. –  Apr 25 '13 at 08:12
  • @Peter Lawrey Sir please look once this query http://stackoverflow.com/questions/16055441/computing-method-call-stack-size – Nikhil Agrawal Apr 25 '13 at 08:16
  • 1
    Trying to detect that you are about to run out of stack like trying to detect you are about to run out of memory generally leads to madness. There a few problems with doing this a) it is difficult to determine accurately b) the code you use to detect it could cause a problem when it wouldn't other wise happen c) it is unlikely you can do anything useful with this information. It is usually better to avoid excessive recursion, or memory consumption in the first place. – Peter Lawrey Apr 25 '13 at 10:15
1

Link to my answer. Simplest ways to cause stack overflow in C#, C++ and Java

This Explaination is the basic reason behind StackOverflowException for languages java, C, C++.

Stackoverflow exception is caused genrally in any language due to recursive method calls.

Suppose you have an method which is calling itself or any other method for an infinite recursive loop then it will cause a Stacoverflowexception. The reason behind this is the method call stack gets filed and it wont be able to accommodate ant other method call.

Method call stack looks like this picture.

enter image description here

Explanation -- Suppose Main method has five statements And third method has an call to methodA, then the execution of main method gets paused at statement3 and MethosA will be loaded into call stack. Then method A has a call to methodB. So methodB also gets loaded into the stack.

So in this way infinite recursive calls makes call stack getting filled. So it cant afford any more methods. So it throws StackOverflowException.

And how to encounter it See this link

Computing method call stack size for checking StackOverflowException

I am also looking for solution of my this query.

Community
  • 1
  • 1
Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
  • Ah nice nice.. I thought StackOVerflow is associated directly to memory that is used to store objects. Interesting is that I dont have any recursive functions in my application... not to my knowledge. perhaps there is some indirect recursion Im not aware of, thanks :) – Erich Jagomägis Apr 25 '13 at 08:12
  • @Erich Jagomagis. The main basic reason stackoverflow exception is recursion only. Calling one and the same thing again and again. Always happy to help. – Nikhil Agrawal Apr 25 '13 at 08:14