title: memory allocation in collection of 1 million references in java
body: <p>I created an ArrayList of 1 million MyItem objects and the memory consumed was 106mb(checked from Task Manager) But after adding the same list to two more list through addAll() method ,it takes 259mb. My question is I have added only the references to list , no new objects are created after that 1 million.why does the memory consumption increase eventhough LinkedList has been used (as it wont require contiguous memory blocks so no reallocation will be made)? </p>
  
  <p>How to achieve this efficiently?  Data passes through various lists in my program and consuming more than 1GB of memory.Similar scenario is presented above.    </p>
  
  <pre><code>public class MyItem{
  private String s;
  private int id;
  private String search;
  
  public MyItem(String s, int id) {
      this.s = s;
      this.id = id;
  }
  
  public String getS() {
      return s;
  }
  
  public int getId() {
      return id;
  }
  
  
  public String getSearchParameter() {
      return search;
  }
  
  public void setSearchParameter(String s) {
      search = s;
  }
  }
  
  public class Main{
      public static void main(String args[]) {
          List&lt;MyItem&gt; l = new ArrayList&lt;&gt;();
          List&lt;MyItem&gt; list = new LinkedList&lt;&gt;();
          List&lt;MyItem&gt; list1 = new LinkedList&lt;&gt;();
  
          for (int i = 0; i &lt; 1000000 ; i++) {
              MyItem m = new MyItem("hello "+i ,i+1);
              m.setSearchParameter(m.getS());
              l.add(i,m);
          }
  
          list.addAll(l);
  
          list1.addAll(l);
          list1.addAll(list);
  
          Scanner s = new Scanner(System.in);
          s.next();//just not to terminate 
      }
  }
  </code></pre>