title: PhoneStateListener memory leak - android
body: <p>I am having issue with memory after running many times my app. </p>
  
  <p><b>java.lang.OutOfMemoryError: bitmap size exceeds VM budget</b></p>
  
  <p>I figure I was leaking memory somehow so I did a DUMP HPROF file and used the MAT tool to figure out what was wrong.
  It turns out that after running like 5 times the app and quiting, I find 5 instances of my Activity and 5 instances of PhoneStateListener.</p>
  
  <p>If I remove the call to PhoneStateListener, I don't have that issue anymore and I see just 1 instance of my Activity.</p>
  
  <p>The question is, how do I resolve this?</p>
  
  <p>Thanks</p>
  
  <p>Daniel</p>
  
  <hr>
  
  <p>Here is my code:</p>
  
  <p>OnCreate method: </p>
  
  <pre><code>telephonyManager.listen(mPhoneListener,
                  PhoneStateListener.LISTEN_SERVICE_STATE
                  | PhoneStateListener.LISTEN_SIGNAL_STRENGTH
                  | PhoneStateListener.LISTEN_CALL_STATE
                  | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
                  | PhoneStateListener.LISTEN_DATA_ACTIVITY); 
  </code></pre>
  
  <p>on the Activity class:</p>
  
  <pre><code>PhoneStateListener mPhoneListener = new PhoneStateListener() {
          public void onCallStateChanged(int state,
                  java.lang.String incomingNumber) {
          //do something
      }
  }
  </code></pre>
  
  <p>on onDestroy method:</p>
  
  <pre><code>telephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);        
  mPhoneListener = null;
  </code></pre>