Warning: Do Not Place Android Context Classes in Static Fields; This Is a Memory Leak (And Also Breaks Instant Run)

Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

Simply pass it as a parameter to your method. There is no sense in creating a static instance of Context solely for the purpose of starting an Intent.

This is how your method should look:

public static void log(int iLogLevel, String sRequest, String sData, Context ctx) {
if(iLogLevel > 0) {

Intent intent = new Intent(ctx, LogService.class);
intent1.putExtra("UPDATE_MAIN_ACTIVITY_VIEW", "UPDATE_MAIN_ACTIVITY_VIEW");
ctx.startService(intent);
}
}

Update from comments on question: Cascade the context from the initiating activity (via constructor parameters or method parameters) right up to the point you need it.

Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

I found the solution to this in the answer to a similar question answered by CommonsWare

I quote

The quoted Lint warning is not complaining about creating singletons.
It is complaining about creating singletons holding a reference to an
arbitrary Context, as that could be something like an Activity.
Hopefully, by changing mContext = context to mContext =
context.getApplicationContext(), you will get rid of that warning
(though it is possible that this still breaks Instant Run — I cannot
really comment on that).

Creating singletons is fine, so long as you do so very carefully, to
avoid memory leaks (e.g., holding an indefinite static reference to an
Activity).

So Google is not actually contracting itself. To fix this, if this.getApplicationContext is supplied as a parameter for the context, then there will be no memory leak.

So in essence, ignore the warning and supply this.getApplicationContext as a parameter for the context.

Do not place Android context classes in static fields; this is a memory leak - Lint Warning for static View

Do not put widgets in static fields.

Options include:

  1. Delete this class. Move all of this logic into the activity (or fragment), where you have direct access to the widgets.

  2. Use an event bus (LocalBroadcastManager, greenrobot's EventBus, etc.). Have your code here post messages on the bus when the state changes. Have your UI (activity or fragment) subscribe for messages on the bus and update the widgets.

  3. Have your activity/fragment hold an instance of CommentsAudioPlayer, and make the fields in CommentsAudioPlayer non-static.

Of the three, the first option would be simpler, cleaner, less memory-intensive, and faster to execute.

Do not place Android context classes in static fields; this is a memory leak

About Static issue: let just say you are referencing your service bnm from another class and your service has been destroyed by the OS but the static object(bnm) is still in use by some activity so this will hold on the service context from garbage collection unless you set your bnm reference inside your activity to null and this will leak all the application's resources

Solution :

The optimal option is use BindService in this way you will get the more control over your service through the object of service , in service use IBinder

class MyService..{
public BeaconNotificationsManager bnm;
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

// inside service class
public boolean getStatus(){
return bnm==null;
}
}

So when you bind a service , you will get the binder object which can further give you the service object and use your function to check nullity

1.) Create a ServiceConnection object

  private ServiceConnection mConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
bnmNull= mService.getStatus(); // bnm status
}

2.) Bind a Service using ServiceConnection object created in first step

    Intent intent = new Intent(this, MyService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

,so then simply have a function in your class 'getStatus' and call it with the object retrieved through the binder check out the link for code example

Memory leak for static declaration of context and INSTANCE , how do I alter it?

It's safe to store application context in a static field, you can simple call context.getApplicationContext() on any context reference you get before storing it in a static field.

The application context is a singleton anyway and you cannot leak it.



Related Topics



Leave a reply



Submit