I have a question regarding a memory leak in my Java Code:
I have a service that runs every second (in production it runs every minute)
When I run the Android Studio Memory-Profiler, I can see that every time this code executes, the "Allocated Memory" value goes up by 6
. So I guess this code is leaking? Obviously it leeks very slowly, but I wonder what is the problem with that code? What can I do to fix this? Or is this "normal" behaviour?
SyncService.java
public class SyncService extends Service {
@Override
public void onCreate() {
Context context = getApplicationContext();
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(!Helper.isNetworkAvailable(context)){
Log.i(TAG, "Offline");
return;
}
// ...
}
}, 1000, 1000);
}
}
Helper.java
public class Helper {
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
If I comment-out the call to Helper.isNetworkAvailable(context)
the allocated value stays still.