title: Memory Leak in practical example
body: <p>I am constantly struggling with identifying memory leaks. I guess I have several memory leaks in my project <a href="https://github.com/momentummodules/CircularProgressView" rel="nofollow noreferrer">circular progress view</a>.</p>
  
  <p>One of my guesses is I have a memory leak in the internal class <code>FadeRunnable</code>. 
  But to be honest I don't know exactly how to find out if this is exactly the source of the problem. Well, when I do the usual scenario and switch the orientation I see an increase of the memory usage as shown below. And if I comment out the usage of the <code>FadeRunnable</code> class the steps are smaller (<em>but still there, so I guess that's not the only leak</em>)</p>
  
  <p><a href="https://i.stack.imgur.com/IKdYf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IKdYf.png" alt="memory steps"></a></p>
  
  <p>Once I analyze the heap dump, I see something. But actually I don't know what the values mean. The things I do is</p>
  
  <ol>
  <li>Change orientation many times</li>
  <li>Open heap dump and sort by 'Retained Size'</li>
  <li>Now when I click on "CircularProgressView' I see 8 rows in the right area. I guess this means there are 8 instances of the 'CircularProgressView' leaked and living somewhere as orphan in the memory.</li>
  </ol>
  
  <p><strong>Is this correct? If so, how can I find out in the dump information (<em>I guess somewhere in the lower pane</em>) where this object is saved/held.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/7yBRS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7yBRS.png" alt="heap dump"></a></p>
  
  <p><strong>I would love to have a step-by-step explanation how to find out if and which object is leaking some memory.</strong></p>
  
  <p>All of the code of the suspected view can be found in this class.</p>
  
  <p><a href="https://github.com/momentummodules/CircularProgressView/blob/master/circularprogressview/src/main/java/momentum/circularprogressview/CircularProgressView.java" rel="nofollow noreferrer">https://github.com/momentummodules/CircularProgressView/blob/master/circularprogressview/src/main/java/momentum/circularprogressview/CircularProgressView.java</a></p>
  
  <p>But also feel free to check out the full project for deeper insight and if you want to play around with it.</p>
  
  <p>Thanks in advance!</p>
  
  <h1>UPDATE</h1>
  
  <p>The code link from above shows the fixed code of the mem-leaking inner class. The following snippet shows the original mem-leaking code that should never be used like that</p>
  
  <pre><code>/**
   * Mem-leaking code, for fixed code see repository link
   * https://github.com/momentummodules/CircularProgressView/blob/master/circularprogressview/src/main/java/momentum/circularprogressview/CircularProgressView.java
   */
  public class CircularProgressView extends View
  {
      ...
      private Thread fadeThread = null;
      ...
  
      ...
      class FadeRunnable implements Runnable
      {
          @Override
          public void run()
          {
              ...
          }
      }
      ...
  
      ...   
      private void startFade(boolean fadeIn)
      {
          // check existing
          if(this.fadeThread != null)
          {
              // check if fade is already running
              switch(this.fadeThread.getState())
              {
                  case TERMINATED:
                  case NEW:
                      this.fadeThread = null;
                      break;
                  case RUNNABLE:
                  case BLOCKED:
                  case TIMED_WAITING:
                  case WAITING:
                      return;
              }
          }
          // create new
          this.fadeThread = new Thread(new FadeRunnable(fadeIn, this.fadeTime));
          this.fadeThread.start();
      }
  }
  </code></pre>