android


Android: Memory Leak


I am using the code below in my activity class:
public static Activity list_Addresses_Activity;
And in my onCreate I used from this :
list_Addresses_Activity = this;
But it throws an error stated below:
Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)
I need to use it from static because I will use from this in my Service class.
My CloseActivies.class :
public class CloseActivies {
Activity a;
Activity b;
Activity c;
protected void CLSActivities(Activity ListAddresses, Activity ListOrder, Activity SendReports) {
a = ListAddresses;
b = ListOrder;
c = SendReports;
if (ListAddressesActivity.FlagLiveAddress && a != null) {
Log.e("ADASFSDAGWEG", "X");
a.finish();
ListAddressesActivity.FlagLiveAddress = false;
}
if (ListOrderActivity.FlagLiveOrder && b != null) {
Log.e("ADASFSDAGWEG", "Y");
b.finish();
ListOrderActivity.FlagLiveOrder = false;
}
if (SendReportsActivity.FlagSendReport && c != null) {
Log.e("ADASFSDAGWEG", "Z");
c.finish();
SendReportsActivity.FlagSendReport = false;
}
}
protected void CLSActivities() {
if (ListAddressesActivity.FlagLiveAddress && a != null) {
Log.e("ADASFSDAGWEG", "X");
a.finish();
ListAddressesActivity.FlagLiveAddress = false;
}
if (ListOrderActivity.FlagLiveOrder && b != null) {
Log.e("ADASFSDAGWEG", "Y");
b.finish();
ListOrderActivity.FlagLiveOrder = false;
}
if (SendReportsActivity.FlagSendReport && c != null) {
Log.e("ADASFSDAGWEG", "Z");
c.finish();
SendReportsActivity.FlagSendReport = false;
}
}
}
It will cause a memory leak as your service class is working on a separate thread and passing a static reference to your activity will keep the instance in memory even if the activity is dismissed and not garbage collected, a safer way to do this is passing a reference of your activity as a parameter to your service and storing it in a weakreference something like this
public class MyIntentService extends IntentService {
private final WeakReference<Context> mContextWeakReference;
public MyIntentService() {
super("MyIntentService");
}
public static void startActionFoo(Context context) {
mContextWeakReference = new WeakReference<>(context);
Intent intent = new Intent(context, MyIntentService.class);
context.startService(intent);
}
#Override
protected void onHandleIntent(Intent intent) {
Context context = mContextWeakReference.get();
if(context != null) {
//do your work as since context is not null means
//activity is still present and if activity is dismissed
//context will come null
}
}
}
If you need activity reference for one of its static variable you can pass it in intent extras, or you want to call a static function of activity, a broadcast receiver would be a better choice to do this.
If you are starting service from activity and use some data of activity in that service. You can pass them in intent.
Intent intent = new Intent(this,MyService.class);
intent.putExtra("data", "some_value");
startService(intent);

Related Links

Obeying android screen orientation-lock prior to v4.3
how to open a specific window on notification click in titanium android
Calling notifyDataSetChanged from instantiateItem()
ExpandableListView start New Activity
How to remove the values stored in hash map from shared preference android
Which parser to use? [closed]
Webview crashing android app on startup
How to detect when the device switch from portrait to landscape mode?
Android- Rounding of Image OutOfMemoryError
How do I test the user name in android [closed]
How do I fix or debug “Error: Android Packager: [app] Cannot create new key or keystore” in IntelliJ
Android : Do not reload Imageview when it has been already loaded in listview
reading images from memory card
RunOnUiThread inside loop efficiency
How to use library jar after import to existing project
How on button click show another layout (xml) with their class?

Categories

HOME
httparty
string
formal-languages
angular2-material
web-hosting
onclick
jetty
python-multithreading
code-coverage
google-cloud-datalab
opengl-3
angular-dart
gkturnbasedmatch
symmetricds
mongodb-csharp
azure-powershell
scsi
feathersjs
google-content-api
topology
sar
cgbitmapcontext
postscript
php-mysqlidb
jquery-callback
appirater
nstimer
exchangewebservices
ceylon
quantlib
uitapgesturerecognizer
zend-debugger
cognos-bi
database-comparison
jbutton
memory-address
catch-unit-test
code-snippets
classcastexception
sql-delete
parse-tree
sessionstorage
metaprogramming
metawidget
apache-directory
jide
servermanager
jboss-4.2.x
twilio-click-to-call
activeperl
qtranslate
wpas
git-flow
libjpeg
swingworker
grails-plugin
omxplayer
respondcms
omniauth-facebook
mediametadataretriever
deep
turbo-prolog
sklearn-pandas
html-to-pdf
database-relations
invoke-sqlcmd
aspen
kendo-upload
atan2
iokit
rewrite
mod-proxy
nutiteq
uncaught-typeerror
ssis-data-flow
paw
nsmenu
jqmodal
unix-socket
mintty
buffering
drupal-nodes
multitasking
rfc5322
ora-06550
light
sphinxql
kendo-dataviz
mvc-editor-templates
cnf
poker
font-awesome-4.0.0
apache-abdera
twisted.web
device-width
intersect
sentestingkit
jquery-blockui
garbage
fusefabric
luabind
undefined-index
rowlex
blender-2.49
skype4java
cldc
xpsdocument
technical-debt
blind
pnrp

Resources

Encrypt Message