简体   繁体   English

简单的Android应用程序进行OOM

[英]Simple Android application going OOM

I have a simple application, collecting and showing Wifi scan results with a few extra features, no intense CPU/RAM usage. 我有一个简单的应用程序,它收集并显示具有一些额外功能的Wifi扫描结果,并且没有占用过多的CPU / RAM。

Problem is while my Thread is running (This thread scan and outputs results via a ExpandableListView), my memory is being filled up avg 70Mb at a time. 问题是我的线程正在运行(此线程扫描并通过ExpandableListView输出结果)时,我的内存一次被平均填满70Mb。

I noticed when reviewing the memory dump that my objects I create exist after they are cleared. 在查看内存转储时,我注意到我创建的对象在清除后存在。

eg a simple example of what I have implemented 例如我实现的简单示例

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));
    }
}

Please do not worry about any syntax,etc issues, this is just for understanding of my problem. 请不要担心任何语法等问题,这只是为了理解我的问题。

This piece of code is run by a Thread , sleeping every 500ms to rescan 这段代码由一个Thread运行,每500ms休眠一次以重新扫描

In this example, after returnList() has finished executing, the ArrayList<> List , new AP(param...) will remain as objects, taking up memory. 在此示例中, returnList()完成执行后, ArrayList<> Listnew AP(param...)将保留为对象,占用内存。

My thread loop is defined by: 我的线程循环定义为:

//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

UPDATE: 更新:

I resorted to clearing globally declared Lists and a scanResultCollection which contain +/- 9 items, this decreases memory taken up by 40Mb. 我诉诸于清除全局声明的List和一个包含+/- 9个项目的scanResultCollection,这减少了40Mb占用的内存。

使用crashlytics库..这将帮助您了解应用程序中发生内存泄漏的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM