Android的堆碎片战略? [英] Android Heap Fragmentation Strategy?

查看:200
本文介绍了Android的堆碎片战略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OpenGL的Andr​​oid应用程序,使用的内存相当数量设立一个复杂的场景,这显然会导致显著堆碎片。即使没有内存泄漏是不可能的破坏,并创建应用程序没有它耗尽内存,由于碎片。 (碎片肯定是问题,而不是泄漏)

I have an OpenGL Android app that uses a considerable amount of memory to set up a complex scene and this clearly causes significant heap fragmentation. Even though there are no memory leaks it is impossible to destroy and create the app without it running out of memory due to fragmentation. (Fragmentation is definitely the problem, not leaks)

这会导致一个重大的问题,因为Android有破坏和创造在同一VM /堆这显然会导致活性崩溃活动的习惯。作为一项战略,以对付这个我已经使用了以下技术:

This causes a major problem since Android has a habit of destroying and creating activities on the same VM/heap which obviously causes the activity to crash. As a strategy to counter this I have used the following technique:

@Override
protected void onStop() {
    super.onStop();
    if(isFinishing()) {
        System.runFinalizersOnExit(true);
        System.exit(0);
    }
}

这保证了当活动结束它会导致一个完整的虚拟机停机,并在活动开始,因此下一次它得到一个新的未分段堆。

This ensures that when the activity is finishing it causes a complete VM shutdown and therefore next time the activity is started it gets a fresh unfragmented heap.

注:我知道这是不是Android的方式,但考虑到垃圾收集器是不可压缩也不可能不断地重新使用堆

此techinque不实际工作一般,然而它并没有活动时在非精加工模式被破坏,然后重新创建工作

This techinque does actually work in general, however it doesn't work when the activity is destroyed in a non-finishing mode and then re-created.

有没有人有关于如何处理堆degredation什么好的建议?

Has anyone got any good suggestions about how to handle the degredation of the heap?

还要注意:减少内存消耗是不是一个真正的选择,或。活动实际上并没有使用那么多的内存,但堆(和本机堆)似乎得到容易分散,可能是由于一些large'ish内存块

推荐答案

碎片几乎都是一种病态的分配格局的结果。频繁创建和销毁大型对象。与更小的物体可以一起坚持(或至少具有不同使用寿命) - 被创建在堆中孔。

Fragmentation is almost always a consequence of an ill conditioned allocation pattern. Large objects are frequently created and destroyed. In conjunction with smaller objects may persisting (or a least having a different lifetime) - holes in the heap are created.

唯一的工作不成prevention在这样的场景是:prevent具体的分配模式。这通常可以通过汇集大对象来完成。如果全成,应用程序将值得庆幸的是承认这一点的,更好的执行速度。

The only working fragmentation prevention in such scenarios is: prevent the specific allocation pattern. This can often be done by pooling the large objects. If successfull, the application will thankfully acknowledge this with a much better execution speed as well.

@edit:还没有更具体的你的问题:如果应用程序重新启动后的堆尚未空的,所以还有什么保留在堆上?你证实,内存泄漏它不是一个问题,但是这是,什么它似乎。由于您使用的是OpenGL的 - 可能是可能是,一些本地的包装幸存下来,因为OpenGL的ressources没有妥善处理完毕?

@edit: yet more specific to your question: if the heap after a restart of the application is yet not empty, so what is there to remain on the heap? You confirmed that its not a problem of a memory leak, but this is, what it seems. Since you are using OpenGL - could it possibly be, some native wrappers have survived, because the OpenGL ressources have not properly been disposed?

这篇关于Android的堆碎片战略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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