为了增加它的小堆栈大小创建Android的手动UI线程 [英] Create the UI thread manually in Android in order to increase its small stack size

查看:163
本文介绍了为了增加它的小堆栈大小创建Android的手动UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主UI线程StackOverflowErrors(<挣扎href=\"http://stackoverflow.com/questions/16843357/what-is-the-android-ui-thread-stack-size-limit-and-how-to-overcome-it\">related问题)。我的应用程序的目标Android 2.3以上版本。在Android 4,一切都很好,但我对2.3.3越来越崩溃视图布局时绘制。

I'm struggling with StackOverflowErrors in the main UI thread (related question). My app targets Android 2.3+. On Android 4 everything is fine, but I'm getting crashes on 2.3.3 when the view layout is drawn.

答案显然是优化我的看法布局,减少嵌套视图的数量。这是一个问题,因为大量的意见是由于我使用的标签支持库和片段 - 这是推荐的方法。我不想改变我的设计和丢弃片段,只是因为一个旧版本的操作系统有一个小的堆栈。

The obvious answer is to optimize my view layout and reduce the number of nested views. This is a problem because the large number of views is due to me using the support library and fragments for tabs - which is the recommended approach. I don't want to change my design and drop fragments simply because an older ver of the OS has a small stack.

我在寻找方法来提高UI线程的堆栈大小。我见过 的答案,这是不可能的,据我所知,没有在明显没有简单的设置这样做。但是,我还是不满意,认为没有手动解决方法。

I'm looking for ways to increase the stack size of the UI thread. I've seen answers that this is not possible, and I understand that there is no simple setting in the manifest to do so. But, I'm still not satisfied that there is no manual workaround.

我的最好的想法迄今:

具有较大堆栈中创建一个新的工作线程(Thread类)。有可能增加堆栈大小为工作线程。然后,在某种程度上改变这个线程新的UI线程。怎么样?

Create a new worker thread (Thread class) with a larger stack. It is possible to increase the stack size for worker threads. Then, transform this thread somehow to the new UI thread. How?


  • 通过对<一个Android的源浏览href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.3_r1/android/app/Activity.java?av=f\">android.app.Activity.attach(..)我见过一个活动附着于UI线程。或许有一些方法来代替它附连到螺纹和其更改为我的新UI线程

  • Browsing through the Android sources for android.app.Activity.attach(..) I've seen that an activity attaches itself to a UI thread. Maybe there is some way to replace the thread it is attached to and change it to my "new" UI thread

去得更深一些,以<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.3_r1/android/app/ActivityThread.java?av=f\">android.app.ActivityThread.startActivityNow(..),也许我就可以手动启动我的活动。一旦活动被创建,它会自动将其自身附加到它创建的线程。也许,如果我运行这个从我的新的UI线程,它会奏效。

Going a little deeper to android.app.ActivityThread.startActivityNow(..), maybe I'll be able to start my activity manually. Once the activity is created, it attaches itself automatically to the thread it is created on. Maybe if I run this from my new UI thread, it will work.

如果我创建一个新的UI线程,我将不得不手动创建活套它。我得到的公司需要从<一个创造了什么感href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.3_r1/android/app/ActivityThread.java?av=f\">android.app.ActivityThread.main(..)

If I create a new UI thread, I will have to manually create the Looper for it. I'm getting a sense of what's need to be created from android.app.ActivityThread.main(..)

其他的想法:


  • 手动抓住的StackOverflowError当它发生移动异步调用,使他们有一个更小的堆栈。我已经看到了这个想法不错<一个href=\"http://stackoverflow.com/questions/6705425/fixing-the-stackoverflow-error/14885190#14885190\">here但我无法使其工作。

NDK线程(通过JNI)有很大的筹码。我想过用它们为我好,但我看不出有什么办法做到这一点。通过JNI所有来电都在自己的线程,所以我不能看我怎么可以使用NDK线程UI进行访问。

NDK threads (via JNI) have large stacks. I thought about using them for my benefit but I see no way to do this. All calls via JNI are performed on their own thread so I can't see how I can use NDK threads for UI access.

所以..

除了说这是一个非常糟糕的主意干脆,其他任何建议或提示吗?你能找到任何人谁没有类似的东西?我做不到。

Except for saying that this is a very bad idea altogether, any other suggestions or tips? Can you find anyone who did something similar? I couldn't..

推荐答案

CommonsWare是大错特错了。仔细检查表明,在Android 2.X的默认堆栈大小12KB是在Android的4.x版已增加到16KB。这意味着核心团队意识到了这个小叠问题,并固定它。遗憾地说,但这些额外的4KB是什么使死机所有的差异。

CommonsWare is sadly mistaken. Closer inspection shows that the default stack size on Android 2.x is 12KB and in Android 4.x has been increased to 16KB. This means the core team was aware of the small stack issue and fixed it. Sad to say, but these extra 4KB is what makes all the difference in crashes.

此外,如果你使用的是ABS和支持V4公用库 - 这些库时,你正在使用的片段添加到您的布局额外的意见。由于我们大多数人依赖这些库,他说,碎片可以很容易地用1视图中添加简直是不正确。如果您使用的是ABS +支持lib中你将支付每个片段3次。你会开始用最少的5次(适用于ABS布局)。

Furthermore, if you are using common libraries like ABS and support v4 - these libraries add the extra views to your layout whenever you are using fragments. Since most of us rely on these libraries, saying that fragments can easily be added with 1 view is simply incorrect. If you are using ABS+support lib you will pay 3 views per fragment. And you will start off with a minimum of 5 views (for ABS layout).

关于手动创建UI线程,这是可能的,并且可能并不需要根设备或改变其固件。这些也是大胆的声明,该声明可能不正确。

Regarding creating a UI thread manually, this may be possible and will probably not require to root the device or change its firmware. These were also bold statements that are probably incorrect.

最后,我决定对与大叠创造我自己的UI线程。相反,我选择从我的code完全下降的使用碎片。本作总共有5次救了我在我的布局。该片段给你的一切,也可以通过手动添加ViewGroups来完成。这只是多一点code,但没有太复杂。看来,碎片有点没用的,如果你没有使用它们的奢华 - 这是削减首位

Finally, I've decided against creating my own UI thread with large stack. Instead, I've chosen to completely drop the use of fragments from my code. This saved me a total of 5 views in my layout. Everything that fragments give you, can also be done by adding ViewGroups manually. It's just a little more code, but nothing too complex. It seems that fragments are a little useless and if you don't have the luxury of using them - that's the first place to cut.

这篇关于为了增加它的小堆栈大小创建Android的手动UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆