I need to test if this IntentService
is running or not and have created the static
method isInstanceCreated()
. It return as you see true or false.
I'm trying to learn what happens hope you can follow me..
Now, what really happens here if:
1- I call isInstanceCreated()
from a BroadcastReceiver
regarding GC
(my thoughts are when BroadcastReceiver
finish and eligible for GC, the IntentService
is also eligible for GC)
2- if I call isInstanceCreated()
from Application
class regarding GC
(my thoughts are IntentService
is GC when Android kill Application)
3- The accessor of the static class method will hold the reference something..???
4- I know that static final fields are hard coded by the compiler and when accessing static final fields the static stuff in the class like static blocks and fields are never loaded. But what happens here when I call isInstanceCreated()
, the static stuff are loaded from top to bottom right, as it is written in the code. But what about memory consumption when I call isInstanceCreated()
. When I instantiate class with New
or class.forname
and instance is created and memory is allocated for the hole class right. When i call isInstanceCreated()
are the entire class also taking up memory (nobody can access the non-static stuff of course becuase it need instantiating first). Hope you follow my learning curve and can give some answer.
public class MyIntentService extends IntentService {
private static boolean stopNow;
private Integer someInt = 10;
private static MyIntentService instance = null;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public MyIntentService() {
super("MyIntentService");
}
public static boolean isInstanceCreated(){
return instance != null;
}
@Override
protected void onHandleIntent(Intent intent) {
}
public boolean someMethod(){
{
// much more methods
}
I know this works for me but getRunningAppProcesses bad solution by Dianne Hackborn
To determine whether your service is still running, take a look at the answers to this question. Please note that this is different from the getRunningAppProcesses
solution that you linked to.
In the current implementation of your class, isInstanceCreated()
doesn't tell you whether the service is running; it only tells you whether an instance of MyIntentService
exists. The static member instance
will leak the service object if it's not set to null (e.g., in onDestroy()
).
Now, what really happens here if:
1- I call isInstanceCreated() from a BroadcastReceiver regarding GC (my thoughts are when BroadcastReceiver finish and eligible for GC, the IntentService is also eligible for GC)
2- if I call isInstanceCreated() from Application class regarding GC (my thoughts are IntentService is GC when Android kill Application)
Calling isInstanceCreated()
has no effect on GC eligibility because it doesn't create or destroy any references.
3- The accessor of the static class method will hold the reference something..???
I don't know what you mean by "static class method". The reference to MyIntentService
is held by the static member instance
. If you set instance
to null, then MyIntentService
will be GC eligible when the service finishes running and Android removes its own reference(s) to it.
4- [...] When i call isInstanceCreated() are the entire class also taking up memory [...]
When you reference MyIntentService
the first time, the class loader loads the class into memory and allocates memory for its static members. Since you only have two static members in your class, the memory usage will be very low.