37353

Android studio say static field will leak contexts

Question:

<a href="https://stackoverflow.com/questions/44169270/do-not-place-android-context-classes-in-static-fields-this-is-a-memory-leak" rel="nofollow">Here</a> is a similar question but I think it is not identical

And I read Setting up request queue tutorial <a href="https://developer.android.com/training/volley/requestqueue.html" rel="nofollow">here</a>

In this page they write the following code

public class MySingleton { private static Context mCtx;

And I wrote same code in my project

public class VolleySingleton { private static Context mContext;

Android studio say “Do not place Android context classes in static fields; this is a memory leak”.<br /> What does it mean? And why dos the official android developer website use such kind of code?

Answer1:

<blockquote>

Android studio say static field will leak contexts

</blockquote>

Let say You are referencing your class <strong>MySingleton</strong> from another class and your <strong>MySingleton</strong> Class has been destroyed by the OS but the static Context(mContext) is still in use by some activity so this will hold on the context from garbage collection unless you set your MySingleton reference inside your activity to null and this will leak all the application's resources

Answer2:

try this? `

class VolleySingleton { @SuppressLint("StaticFieldLeak") private static Context mContext; }`

Recommend

  • Modifying an array passed as an argument to a function in Swift
  • Javascript and singleton pattern
  • how to test picasso using unit-test and mockito
  • I keep getting the SQLException error with this
  • Make image accessible from other ViewController
  • Migration solution for singletons in an OSGI environment
  • Missing permission to control media error on Android 5/L
  • Insert Statement
  • Android getAssets in Common class gives “The method getAsssets() is undefined for the type Context”
  • IllegalArgumentException: The servlets named [HelloWorlds] and [Hello] are both mapped to the url-pa
  • JsonConverter: Get parent object in ReadJson, without $refs
  • Asynchronous Sockets - Handling false socket.AcceptAsync values
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • How do I translate LR(1) Parse into a Abstract syntax tree?
  • ConnectivityManager.CONNECTIVITY_ACTION deprecated
  • Deploying a CodeRush plugin from the Community Site
  • .NET video play library which allows to change the playback rate?
  • Best practice to eliminate magic numbers within a member function
  • How to pass solution folder as parameter in command line arguments (for debug)?
  • Iron Router: How do I send data to the layout?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Wrong row deleted from custom listview with spinner
  • Detecting null parameter in preprocessor macro
  • Spring: No transaction manager has been configured
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • Javascript Callbacks with Object constructor
  • sending/ receiving email in Java
  • Javascript convert timezone issue
  • Javascript + PHP Encryption with pidCrypt
  • Weird JavaScript statement, what does it mean?
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Getting Messege Twice Using IMvxMessenger
  • need help with bizarre java.net.HttpURLConnection behavior
  • Checking variable from a different class in C#
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?