in Education by
I have been doing Java development for a long time, but I am ashamed to say I still don't have a good grasp on how memory leaks actually takes place. I however have no experience with Android development. Take the following code for example taken from this presentation: I have the following questions (please note that I am new to Android, but this example is not primarily focused on Android) How is memory leak even possible here ? The author says that its because the inner class is not marked as static and it will hold a reference to the outer class. So if I keep creating objects of the outer class like so: MainActivity one = new MainActivity(); one.onCreate(bundle); MainActivity two = new MainActivity(); two.onCreate(bundle); MainActivity three = new MainActivity(); three.onCreate(bundle); How does it matter ? If I am not wrong about how static references work, then only one object of LeakClass is created, right ? Unless a separate classloader is involved, right ? Is the memory leak only possible here because of how Android works ? If I were to manually create these objects then this would not be a problem right ? Please help me with an ELI5 explanation here, I am really unable to understand how this works. Thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Because LeakClass is an inner class, it has an inherent reference to the instance of the outer class that owns it. Since you're storing that value in a static variable, that means you're storing a reference to the outer class in that static transitively. Storing an Activity in a static is ALWAYS a leak, and ALWAYS a bad idea. Activities aren't singletons, you cannot assume there will only be one of them. If you don't need that parent reference, you can make it a static inner class. Static inner classes do not have an implicit outer reference, but cannot access member variables of functions of the owning class. Or you can make it a standalone, non-inner class (the two are equally good solutions). If you do need that parent reference, you cannot store it in a static variable without a leak. Figure out another way to make that data available.

Related questions

0 votes
    What is meant by a memory leak in Java, and how to implement it. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    What is meant by a memory leak in Java, and how to implement it. Select the correct answer from above ... ,Core Questions, Core Hadoop MCQ,core interview questions for experienced...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    Which one of the following causes memory leak? (a) Release database connection when querying is complete (b) ... Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I have three same source structure tables. But, I want to load into single target table. How do I do this? Explain in detail through mapping flow....
asked Mar 28, 2021 by JackTerrance
0 votes
    I have been having some issues with LINQ-To-SQL around memory usage. I'm using it in a Windows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I have been having some issues with LINQ-To-SQL around memory usage. I'm using it in a Windows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I have been having some issues with LINQ-To-SQL around memory usage. I'm using it in a Windows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    can anyone please help me out for this question Write a program to create two threads, so one thread will print ... between 11 to 20. Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    can anyone please help me out for this question Write a program to create two threads, so one thread will print ... between 11 to 20. Select the correct answer from above options...
asked Dec 18, 2021 in Education by JackTerrance
0 votes
    I am getting memory leak during migration using Realm (v1.0.2). My code looks like that: let ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have an ETL requirement like: I need to fetch around 20000 records from a table and process each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am writing some code that uses MPI and I was keeping noticing some memory leaks when running it with ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    We've tried jdk 1.7.0_02. Ten days of running give 420 MB memory leak of the following objects: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the below is not a memory leak solution? (a) Code changes (b) JVM parameter tuning (c) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Which one of the following causes memory leak? (a) Release database connection when querying is complete (b ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...