title: Memory leak using autoboxing
body: <p>I was following <a href="https://dzone.com/articles/memory-leak-andjava-code" rel="nofollow noreferrer">tutorial</a> and below was the example for auto boxing memory leak.</p>
  
  <pre><code>package com.example.memoryleak;
  public class Adder {
    public long addIncremental(long l) {
      Long sum=0L;
      sum =sum+l;
      return sum;
    }
    public static void main(String[] args) {
      Adder adder = new Adder();
      for(long ;i&lt;1000;i++)  {
        adder.addIncremental(i);
        }
      }
   }
  </code></pre>
  
  <p>Now, I could understand that unnecessary objects would be created because of autoboxing but how it caused memory leak, the way I understand is that memory leak is caused when you are holding a strong reference to a dead object. Now, in this case once I have came out of the FOR loop there would be no strong references to those <code>Long</code> objects then how it caused memory leak? </p>
  
  <p>Please note I want to understand how it caused memory leak, I know those objects were unnecessary.</p>