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

http request callback leaking memory, or is it?

我的应用程序存在严重的内存泄漏问题。 应用程序(服务)每 30 秒循环一次,下载数据并将其放入数据库。 我正在使用一个似乎正在泄漏内存的回调侦听器,与库无关,因为我同时尝试了 volley 和 httpok,我唯一的猜测是它实际上是 dbhelper。我想我必须在代码周围的某个地方使用弱引用,我试过了,因为我将请求本身重构为不同的 class,我也尝试了 WeakHashMap,但仍然没有成功。我尝试隔离可运行的处理程序(我现在正在使用 weakhandler 库)仍然不行:( 这是求救,我写了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);
    }
}
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);
    }