3

I wrote some simple tree/graph algorithms, but quickly ran into a lot of StackOverflowError exceptions with some pretty small data. It turns out the stack is 8KB by default on my Samsung Galaxy S3, which has 2GB of RAM. My computer 10 years ago had a 1MB stack. The Linux machine machine I'm using right now has 4GB RAM. My phone's RAM is only half the size of my computer's RAM, yet my phone's stack is over 1000 times smaller. Why?

What is the actual technical reason that the developers of Android had to limit the stack much more than other operating systems? E.g is it because some Android devices have a small amount of RAM like 1MB or 10MB? I haven't surveyed the range of devices, but I find it hard to believe that any device would be so small.

Dog
  • 7,707
  • 8
  • 40
  • 74
  • Architectural design decision by Samsung/Google? – Geeky Guy Aug 13 '13 at 20:50
  • Because that's the default stack size (and IIRC it's version and/or device dependent). – Dave Newton Aug 13 '13 at 20:50
  • Check this: http://stackoverflow.com/questions/1859327/android-stack-size – Geeky Guy Aug 13 '13 at 20:53
  • @NikolaDespotoski A stack overflow error may happen if you have too deep a stack too, recursion or no recursion. It's just that computers have had such big stacks in the last decades that overflowing the stack without an infinite loop is practically unheard of these days. But with a 8kb stack, that's not unsurprising at all. Have a View Hierarchy with a depth bordering on 20 and you're sure to trigger this error. – Geeky Guy Aug 13 '13 at 20:56
  • 1
    Closing this as opinion based tells me that there is no reason for this impairment. If there was a reason, you'd write it as an answer instead of voting to close. Also coincidentally, the mod who closed this works for SO, develops for Android, and has no rep. – Dog Aug 13 '13 at 23:09
  • 1
    @Dog They are probably infuriated that you pointed out such a silly problem in their favorite platform. – Harold R. Eason Aug 13 '13 at 23:44
  • 1
    The total amount of RAM of the device is inconsequential to your question. As each process/application has its own max usable RAM, which was hardcoded at a meager 16 MB for older platforms. Even for more modern devices/OSes the max usable RAM would usually only be around 32~48 MB. But 8 KB is indeed quite small, I guess they don't expect/want people to write recursive programs? – Kai Aug 14 '13 at 01:03

1 Answers1

0

Just create a Thread with the stack size you want and run your code in it.

I believe that the default one of 8KB is due to the fact that it is most probably allocated on the heap, and a 32-bit architecture does not have a lot of VRAM to waste on it (with a 2GB memory split it's only 2GB per process available). Allocating a big stack would permanently take it out of the available VRAM of the process. This is different from the traditional native processes where the stack grows from top to bottom while the heap grows from the bottom to the top.

dragonroot
  • 5,653
  • 3
  • 38
  • 63
  • It's set in the firmware. You could change that if you wanted, but between modifying Android and flashing your phone you'd better just refactor your code. – Geeky Guy Aug 13 '13 at 20:57