http请求回调泄漏内存,还是?

java android memory-leaks runnable weak-references

394 观看

1回复

6 作者的声誉

由于我的应用程序的内存泄漏,我遇到了严重的麻烦。应用程序(服务)每30秒循环一次,下载数据并将其放入数据库中。我正在使用一个回调监听器,该监听器似乎正在泄漏内存,与我尝试凌空和httpok时所用的库无关,我唯一的猜测是它实际上是dbhelper。我想我必须在代码的某个地方使用弱引用,我尝试过,当我将请求本身重构到另一个类时,我也尝试了WeakHashMap,但仍然没有成功。我试图隔离可运行的处理程序(我现在正在使用weakhandler lib)仍然没有走:(这是在寻求帮助,我正在编写相同版本的代码4天!

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.badoo.mobile.util.WeakHandler;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import prefs.Settings;
import tools.DBHelper;

public class tempclass extends Service {
    private dbhelperFortempclass dbHelper;
    private WeakHandler mHandler;
    private Runnable runnable;
    private OkHttpClient okHttpClient;

    public tempclass() {
    }


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

        dbHelper = dbhelperFortempclass.getInstance(); //инциируем sql
        mHandler = new WeakHandler();

        okHttpClient = new OkHttpClient();
        executeDownloadsAndWriteToDataBase();
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public int onStartCommand(Intent intent, int flags, int startId) {


        return super.onStartCommand(intent, flags, startId);
    }

    public IBinder onBind(Intent arg0) {
        return null;
    }


    private void executeDownloadsAndWriteToDataBase() {

        runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    downloadData();
                    mHandler.postDelayed(this, 1000);
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                } finally {
                    //also call the same runnable
                    mHandler.postDelayed(this, 20000);
                }
            }
        };

        mHandler.postDelayed(runnable, 30000);
    }

    private void downloadData() throws IOException {


        for (Map.Entry<String, Settings.ListofCurrencies> entry : Settings.MyCurrencyList.entrySet()) {
            DownloadAndWriteToDBExecute(entry.getKey());

        }

    }


    public void DownloadAndWriteToDBExecute(final String whatToGet) {
        Request request = new Request.Builder().url(whatToGet).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                //logger.log(Level.SEVERE, "Failed to execute " + request, e);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()) {
                    DBHelper.execSQL("sql code");
                } else throw new IOException("Unexpected code " + response);

            }
        });

    }


}



import android.content.Context;
import android.database.sqlite.SQLiteDatabase;


public class dbhelperFortempclass {

    private static SQLiteDatabase mydb;
    private static Context myContext;

    private static dbhelperFortempclass dbHelper = null;

    protected dbhelperFortempclass() {
    }


    public static dbhelperFortempclass getInstance() {
        if (dbHelper == null) {
            myContext = MainActivity.getAppContext();
            dbHelper = new dbhelperFortempclass();
        }

        return dbHelper;
    }


    public static void execSQL(String cmd) {
        mydb.execSQL(cmd);
    }
}
作者: AndroidFan 的来源 发布者: 2016 年 1 月 6 日

回应 (1)


0

6 作者的声誉

决定
private void executeDownloadsAndWriteToDataBase() {
        runnable = null; //this is what was reason for the memory leak, the runnable was recreating itself
        runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    downloadData();
                    mHandler.postDelayed(this, 1000);
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                } finally {
                    //also call the same runnable
                    mHandler.postDelayed(this, 20000);
                }
            }
        };

        mHandler.postDelayed(runnable, 30000);
    }
作者: AndroidFan 发布者: 07.01.2016 01:51
32x32