Android Stack Overflow

Hi, I am facing the nasty java.lang.StackOverflowError on an maze generator app running on android.
I get this error only on android, it happens because probably the xss is low in the android jvm.
I tried to add the -Xss1m on the run parameter but it seens it gets no effect …
There is any other place I could fix this in the app ?

Use data recursion instead of code recursion.

Thats a way.
But I wander, how can I change the jvm (dvm) parameters ?
Like GC behavior for example.
Maybe something on AndroidManifest ? I could not find any info about this file…

Some quick googling (when did that become a common verb?) got me to a page on the android developer docs

  • If you spawn a separate thread to do your maze generation in, you can ask it to set a stack size of your choice. (size is in bytes)
  • Note: in the documentation for the relevant constructor it says: a stack size for the new Thread. This has a highly platform-dependent interpretation. It may even be ignored completely. And per some answers to a related stackexchange question, some versions of android/devices do just that, or at least chop the number back to what they consider to be a sensible maximum.

Short version: @pspeed is right. A recursive function is not the right solution, given that you want it to run reliably on various hardware.

As an aside, I’ve never run across the the expression “data recursion” before. References? Or, how does it compare/differ from an iterative solution?

Thats very intesting ! Thank you !
About the code recursive way x data recursion, I think what he means is that there is 2 main ways to make this kind of algorithm, one ( the easier one ) is to use recursion and that is what I am using, the other is to store every stack of the maze in an array and use it in an loop.

So, basically, implement your own stack for state of the generation algorithm? I have seen that done, at least as an example of a quick way to get out of stack recursion issues.

Yeah, code recursion is where you let the code stack do your state tracking. Data recursion is where you let the data do it and iterate.

For example, code recursion to get the root node from a spatial:

public Node getRoot( Spatial s ) {
    if( s.getParent() == null ) {
        return s;
    } else {
        return s.getParent();
    }
}

Data recursion:

public Node getRoot( Spatial s ) {
    while( s.getParent() != null ) {
        s = s.getParent();
    }
    return s;
}

It’s wrong to say you’ve replaced the stack because sometimes the stack is not necessary. However, it is true that if you don’t already have a recursive data structure that you will have to create one and that it will take the place of the stack in code recursion. It comes with other benefits, though.

You can strictly control how the data is allocated and managed. For example, you could decide that if your ‘stack’ gets too deep that you just stop adding new elements. Maybe for some algorithm this is an ok compromise for crashing.

You can sort the ‘stack’ as you go to always do the best paths first. (Think A* and similar.)

It is easier to embed tracking data in your structure without having to add a ton of parameters to your call arguments (and thus the code stack).

The benefits are numerous. I almost always use data recursion unless I know that they code recursion way is both easier and pretty well guaranteed to be of limited depth. (For example, the JME scene graph is never deep so child traversal is pretty safe with code recursion.)

I’d be very surprised if the monkey trap maze generation was not already using data recursion.

Thats ok, I will fix this using your suggestion.
What bugs me though is that it seens the default dvm settings are different from device to device, it would be an good idea to set an equalized stack limit size in your application for example, gc configurations etc, but I really could not find a easy way to do it…
There is an way to specify on the thread creation, but it dosent seen to be easy to do it using jm3, maybe some core change on the android code ?
If anyone knows any information about how to do such thing it would help to write better android apps.