Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

468
Views
Memory-leak free Singleton with context

I am trying to implement the following singleton pattern: SingletonClass.getInstance(context).callMethod()

While there are a variety of tutorials that explain how to make singletons in Kotlin, none of them address the fact that holding a context in a static field will cause memory leaks in Android.

How do I create the above pattern without creating a memory leak?

Update:

Here is my implementation of CommonsWare's solution #2. I used Koin.

Singleton class:

class  NetworkUtils(val context: Context) {

}

Application Class:

class MyApplication : Application() {

    val appModule = module {
        single { NetworkUtils(androidContext()) }
    }

    override fun onCreate() {
        super.onCreate()
        startKoin(this, listOf(appModule))
    }
}

Activity class:

class MainActivity : AppCompatActivity() {

    val networkUtils : NetworkUtils by inject()

}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Option #1: Have getInstance(Context) call applicationContext on the supplied Context and hold that. The Application singleton is created when your process is and lives for the life of the process. It is pre-leaked; you cannot leak it further.

Option #2: Get rid of getInstance() and set up some form of dependency injection (Dagger 2, Koin, etc.). There are recipes for these DI frameworks to have them supply the Application singleton to the singletons that they create and inject downstream.

over 4 years ago · Santiago Trujillo Report

0

When you call the getInstance() for the first time, the Context that you pass to this function is saved forever. So, the context in further getInstance() calls doesn't have anything to do there. I never save this Context.

This is what I am doing:

Create an object in Kotlin and initialize the object with a context as soon as the app starts. Instead of storing the context, I perform whichever operation is required with that context.

object PreferenceHelper {

    private var prefs: SharedPreferences? = null

    fun initWith(context: Context){
        if(prefs == null) this.prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
    }

    fun someAction(){ .... }
}

and inside the Application class:

class MyApp: Application(){
   override fun onCreate(){
      PreferenceHelper.initWith(this)
   }
 }

and later anywhere in the app:

 PreferenceHelper.someAction()

You can do this if you don't need a reference to the Context every time you perform something with the Singleton class.

over 4 years ago · Santiago Trujillo Report

0

I would not store the context in the SingletonClass, I would simply pass the context to each method of the class via dependency injection. Something like:

SingletonClass.callMethod(context)

Define the "static" method in the companion object like that:

 companion object {
        fun callMethod(context: Context) {
            // do Something
        }
    }

Then call it from your activity with:

SingletonClass.callMethod(this)

Hope that it helps :)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!