I am having a serious trouble with my app as its leaking memory. The application (service) is looping every 30 secs, downloading data and putting it into the database. I am using a callback listener which seem to be leaking memory, isn’t about the library as I tried both volley and httpok, my only guess is that it’s actually the dbhelper. I suppose I have to use weakreference somewhere around the code, I tried that, as I refactored the request itself to a different class, I also tried WeakHashMap, still no success. I tried isolating the runnable handler (I am using weakhandler lib right now) still no go :( This is a cry for help, I am writing the same versions of code for 4 days!
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);
}