简单的Android应用程序进行了OOM



我有一个简单的应用程序,收集和显示具有一些额外功能的WiFi扫描结果,没有强烈的CPU/RAM使用。

问题是当我的线程运行时(此线程扫描并通过ExpandableStview输出结果),一次我的内存一次填充AVG 70MB。

我在查看记忆转储时注意到我创建的对象被清除后存在。

例如。我实施的简单示例

private ExpandableListAdapter returnList(Context context, String[] APList) {
    if (bLogging) { //Assume true
        ArrayList<String> List = new ArrayList<>();
        foreach (AccessPoint ap: APList){
            //code initilializing and assigning param variables
            List.add(new AP(param1, param2, param3))
        }
        return (new ExpandableListAdapter(context, List));
    }
}

请不要担心任何语法等问题,这只是为了理解我的问题。

此代码由Thread运行,每个500ms睡到Rescan

在此示例中,在returnList()完成执行后,ArrayList<> Listnew AP(param...)将保留为对象,占用内存。

我的线程循环定义为:

//code before
t = new Thread(new Runnable() {
        public void run() {
            handler = new Handler(context.getMainLooper());
            while (!bStopThread) {
                ThreadCounter++;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (bSafe) {
                            initWiFiArrays();  //AccessPoint Objects are created as shown in example
                            CreateSetAdapter(); //Custom ExpandableListAdapter is created, but declared and instantiated within the method
                            threadRefresh.setText("# Refreshed Times : " + String.valueOf(ThreadCounter));
                            writeResultsToFile();
                        } else
                            stopScan();
                    }
                });
                try {
                    Thread.sleep(scanInterval);
                } catch (Exception x) {
                    x.printStackTrace();
                }
            }
        }
    });
t.start();
//code after

更新:

我诉诸于清除全球声明的列表和包含/- 9个项目的扫描列表,这将记忆减少40mb。

使用crashlytics库..这将帮助您从应用程序中的内存泄漏中知道

最新更新