http请求回调泄漏内存,或者是它?

时间:2016-01-06 19:41:28

标签: java android memory-leaks runnable weak-references

我的应用程序因为泄漏内存而遇到严重问题。 应用程序(服务)每30秒循环一次,下载数据并将其放入数据库。 我正在使用一个似乎泄漏内存的回调监听器,不是关于库,因为我尝试了凌空和httpok,我唯一的猜测是它实际上是dbhelper。我想我必须在代码周围使用弱引用,我试过,因为我将请求本身重构为另一个类,我也尝试过WeakHashMap,但仍然没有成功。我试过隔离runnable处理程序(我现在正在使用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);
    }
}

1 个答案:

答案 0 :(得分:0)

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