方向改变时内存不足错误

时间:2012-12-12 00:59:33

标签: android memory-leaks screen-orientation osmdroid

我有一个使用osmdroid mapactivity的应用程序。

每次屏幕方向的堆大小增加,并且在几次方向更改后,我会收到以下错误:

12-12 00:53:08.990: E/dalvikvm-heap(6712): Out of memory on a 262160-byte allocation.
12-12 00:53:08.990: I/dalvikvm(6712): "filesystem" prio=5 tid=46 RUNNABLE
12-12 00:53:08.990: I/dalvikvm(6712):   | group="main" sCount=0 dsCount=0 obj=0x43057e08 self=0x571583c8
12-12 00:53:08.990: I/dalvikvm(6712):   | sysTid=8674 nice=0 sched=0/0 cgrp=apps handle=1487255264
12-12 00:53:08.990: I/dalvikvm(6712):   | schedstat=( 298265664 165380214 301 ) utm=25 stm=4 core=0
12-12 00:53:08.990: I/dalvikvm(6712):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-12 00:53:08.995: I/dalvikvm(6712):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
12-12 00:53:08.995: I/dalvikvm(6712):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
12-12 00:53:08.995: I/dalvikvm(6712):   at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:449)
12-12 00:53:08.995: I/dalvikvm(6712):   at org.osmdroid.tileprovider.tilesource.BitmapTileSourceBase.getDrawable(BitmapTileSourceBase.java:93)
12-12 00:53:08.995: I/dalvikvm(6712):   at org.osmdroid.tileprovider.modules.MapTileFilesystemProvider$TileLoader.loadTile(MapTileFilesystemProvider.java:142)
12-12 00:53:08.995: I/dalvikvm(6712):   at org.osmdroid.tileprovider.modules.MapTileModuleProviderBase$TileLoader.run(MapTileModuleProviderBase.java:241)
12-12 00:53:08.995: I/dalvikvm(6712):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-12 00:53:08.995: I/dalvikvm(6712):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-12 00:53:08.995: I/dalvikvm(6712):   at java.lang.Thread.run(Thread.java:856)
12-12 00:53:09.020: I/dalvikvm-heap(6712): Clamp target GC heap from 64.436MB to 64.000MB
12-12 00:53:09.050: I/dalvikvm-heap(6712): Forcing collection of SoftReferences for 262160-byte allocation
12-12 00:53:09.095: I/dalvikvm-heap(6712): Clamp target GC heap from 64.436MB to 64.000MB

我有一个包含两个片段的Activity,这两个片段都扩展了一个初始化MapActivity的抽象片段。

我从其他人那里读过很多同样问题的帖子。我最终在抽象片段中实现了以下内容:

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        unbindDrawables(con.findViewById(R.id.main_layout));
        System.gc();
    }

    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }

这是我的onCreateView初始化con:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        con = container;
        return inflater.inflate(R.layout.main_fragment,container, false);
    }

我不确定我还能做些什么。

任何人都可以帮我解决这个问题吗?

由于

2 个答案:

答案 0 :(得分:2)

尝试自己处理屏幕旋转,将以下代码添加到活动

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

并将以下内容添加到清单文件

中的活动代码中

机器人:configChanges = “取向| keyboardHidden |屏幕尺寸”

答案 1 :(得分:1)

始终让操作系统处理您的方向更改!

如果配置发生更改,您的活动将会重新创建,例如方向改变。大多数内存泄漏的原因是存在一些异步操作,其中包含对视图的(隐式/显式)引用,这会阻止您的视图被垃圾回收。例如,如果您使用CountDownTimer或非静态内部可运行。