Memory leak: Confusion between Java Garbage Collector - and Android killing mechanism?
<p>I am newbie to both Java and Android, and currently I am confused about "memory leak" in Android, for example: I have 01 Class, 01 Activity and 01 Interface as following:</p>

<p>Class BackGroundWorker is a singleton, which lives as long as the application lives:</p>

<pre><code>public class BackGroundWorker {
 private IOnEventOccurListener listener = null;
 private static BackGroundWorker instance;
// ....
 public void setListener(IOnEventOccurListener pListener) {
  this.listener =  pListener;
 }
// ....
 public static BackGroundWorker getInstance() {
  //...
  return instance;
 }
}
</code></pre>

<p>The Listener Interface:</p>

<pre><code>public interface IOnEventOccurListener {
 public void onEventOccur();
}
</code></pre>

<p>And the Listener itself (An activity):</p>

<pre><code>public class ShowSomething extends Activity implements IOnEventOccurListener{

 BackGroundWorker bgWorker;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  bgWorker = BackGroundWorker.getInstance();
  bgWorker.setListener(this);
 }

 @Override
 public void onEventOccur() {
  // TODO do something

 }
}
</code></pre>

<p>Now, according to what Romain Guy mentioned <a href="http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html" rel="nofollow">here</a>: 
It’s a memory leak, because there’s a reference to the listener (Activity). So Java GC cannot collect the Activity, even when it’s not in use.   </p>

<p>I was able to solve that problem by WeakReference – but still wonder: 
<strong>In this case, when the device needs more memory, according to Android Dev document, it will “kill” the activity if needed  - assuming that the Activity ShowSomething is “killed” – then what happens ? (It’s still leak according to Romain Guy, and still “killed” )</strong> </p>

<p>I am really confused. Could anybody please explain this ?</p>

<p>Thank you in advanced,<br>
Son</p>
