Java http请求回调是否内存泄漏?

Java http请求回调是否内存泄漏?,java,android,memory-leaks,runnable,weak-references,Java,Android,Memory Leaks,Runnable,Weak References,我的应用有严重的问题,因为它的内存泄漏。 应用程序(服务)每30秒循环一次,下载数据并将其放入数据库。 我正在使用一个回调侦听器,它似乎正在泄漏内存,与库无关,因为我尝试了volley和httpok,我唯一的猜测是它实际上是dbhelper。我想我必须在代码的某个地方使用weakreference,我尝试过,当我将请求本身重构到另一个类时,我也尝试过WeakHashMap,但仍然没有成功。我尝试隔离可运行处理程序(我现在正在使用weakhandler库),但仍然没有成功:( 这是一个呼救,我写了

我的应用有严重的问题,因为它的内存泄漏。 应用程序(服务)每30秒循环一次,下载数据并将其放入数据库。 我正在使用一个回调侦听器,它似乎正在泄漏内存,与库无关,因为我尝试了volley和httpok,我唯一的猜测是它实际上是dbhelper。我想我必须在代码的某个地方使用weakreference,我尝试过,当我将请求本身重构到另一个类时,我也尝试过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);
    }
}
导入android.app.Service;
导入android.content.Intent;
导入android.os.IBinder;
导入com.badoo.mobile.util.WeakHandler;
导入java.io.IOException;
导入java.util.Map;
导入okhttp3.Call;
导入okhttp3.Callback;
导入okhttp3.OkHttpClient;
导入okhttp3.请求;
导入okhttp3.响应;
导入优先设置;
导入tools.DBHelper;
公共类tempclass扩展服务{
私有dbhelperFortempclass dbHelper;
私人韦坎德勒·姆汉德勒;
私人可运行;
私人OkHttpClient OkHttpClient;
公共临时班(){
}
@凌驾
public void onCreate(){
super.onCreate();
dbHelper=dbhelperFortempclass.getInstance();//
mHandler=新的WeakHandler();
okHttpClient=新的okHttpClient();
Executedownloads和WriteToDatabase();
}
公共空间{
super.ondestory();
}
公共int onStartCommand(Intent Intent、int标志、int startId){
返回super.onStartCommand(intent、flags、startId);
}
公共IBinder onBind(意图arg0){
返回null;
}
private void Executedownloads和WriteToDatabase(){
runnable=新的runnable(){
@凌驾
公开募捐{
试一试{
下载数据();
mHandler.postDelayed(这个,1000);
}捕获(例外e){
//TODO:处理异常
e、 printStackTrace();
}最后{
//也可以调用相同的runnable
mHandler.postDelayed(这一数字为20000);
}
}
};
mHandler.postDelayed(可运行,30000);
}
私有void downloadData()引发IOException{
对于(Map.Entry:Settings.MyCurrencyList.entrySet()){
DownloadAndWriteToDBExecute(entry.getKey());
}
}
public void DownloadAndWriteToDBExecute(最终字符串WhatGet){
Request Request=newrequest.Builder().url(whatToGet.build();
Call Call=okHttpClient.newCall(请求);
call.enqueue(新回调(){
@凌驾
公共void onFailure(请求,IOE异常){
//logger.log(严重级别,“无法执行”+请求,e);
}
@凌驾
public void onResponse(Response-Response)引发IOException{
if(response.issusccessful()){
DBHelper.execSQL(“sql代码”);
}否则抛出新IOException(“意外代码”+响应);
}
});
}
}
导入android.content.Context;
导入android.database.sqlite.SQLiteDatabase;
公共类dbhelperFortempclass{
私有静态数据库mydb;
私有静态上下文myContext;
私有静态dbhelperFortempclass dbHelper=null;
受保护的dbhelperFortempclass(){
}
公共静态dbhelperFortempclass getInstance(){
if(dbHelper==null){
myContext=MainActivity.getAppContext();
dbHelper=新的dbhelperFortempclass();
}
返回dbHelper;
}
公共静态void execSQL(字符串cmd){
mydb.execSQL(cmd);
}
}

为什么您认为内存泄漏?我使用了android内存监视器,应用程序的内存从9mb增加到100 MB,然后崩溃)
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);
    }