title: Creating Hashset object in method decreases the stack memory in java
body: <p>I have read that only primitives are stored in Stack memory and objects are stored in Heap memory. In the following program I am recursively calling a method to check the maximum stack size before stackoverflow error occurs.</p>
  
  <pre><code>public class MaxStackSize {
  static int i =0;
  public static void main(String[] args) {
      method1();
  }
  
  public static void method1()
  {
      i++;
      System.out.println(i);
      method1();
  }
  }
  </code></pre>
  
  <p>Here the max output for 'i' is 53481.</p>
  
  <p>If I add HashSet object to the method and populate it:</p>
  
  <pre><code>public class MaxStackSize {
  static int i =0;
  public static void main(String[] args) {
      method1();
  }
  public static void method1()
  {
      HashSet&lt;String&gt; set = new HashSet&lt;String&gt;();
      set.add("one");
      set.add("two");
      set.add("three");
      set.add("four");
      set.add("five");
      set.add("six");
      set.add("seven");
      set.add("eight");
      set.add("nine");
  
      i++;
      System.out.println(i);
      method1();
  }
  }
  </code></pre>
  
  <p>The max output for 'i' is 25403.</p>
  
  <p>For both the cases VM arg is -Xss5m.</p>
  
  <p>If only primitives are stored on stack why does the stacksize decrease when object of HashSet is created.</p>