J
J
jonasas2015-10-19 22:00:29
Android
jonasas, 2015-10-19 22:00:29

Why is a StackOverflowError being thrown?

Strange exceptions are thrown on the user's device:

java.lang.StackOverflowError: stack size 8MB
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:143) 
    at android.os.Looper.loop(Looper.java:195) 
    at android.app.ActivityThread.main(ActivityThread.java:5373) 
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

and
java.lang.StackOverflowError: stack size 8MB
    at android.view.ThreadedRenderer.nInitialize(Native Method)
    at android.view.ThreadedRenderer.initialize(ThreadedRenderer.java:147) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1649) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
    at android.view.Choreographer.doFrame(Choreographer.java:550) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:211) 
    at android.app.ActivityThread.main(ActivityThread.java:5373) 
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

The traces show that there is no large nesting of methods. Also all methods belong to Android SDK.
These exceptions do not repeat on my devices.
What could be the matter and how to establish the cause of these emissions?
Since memory in Java is a heap, it means that large data cannot get on the stack. Only if a large number of links or primitives.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Moiaposhta, 2020-10-11
@Moiaposhta

The stack is different on different devices
This error occurs when a function calls itself recursively (very many times or constantly). You can also create many interface elements, the error will be the same. The simplest way to get rid of an error (of a recursive function) is to implement no recursive functions at all. Those. replace them with nested loops. It will work great. If you create a lot of UI elements, you can increase the stack. Type Android Studio in the search engine - how to increase the size of the allocated heap
Pay attention also to the Handler, if implemented incorrectly, there may be a memory leak
Check if you terminate all threads correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question