title: If Java Strings are immutable and StringBuilder is mutable why they wasting same amount of memory in my code?
body: <p>I ran those code and I got some questions, this kinda got weird.</p>
  
  <p>Using String:</p>
  
  <pre><code>while(true)
  {
      String s = String.valueOf(System.currentTimeMillis());
      System.out.println(s);
      Thread.sleep(10);
  }
  </code></pre>
  
  <p>Using StringBuilder:</p>
  
  <pre><code>StringBuilder s = null;
      while(true)
      {
          s = new StringBuilder();
          s.append(System.currentTimeInMillis());
          System.out.println(s);
          Thread.sleep(10);
      }
  </code></pre>
  
  <p>In both cases they get stuck in 12540 K waste of memory. Running this test on Windows XP SP2.</p>
  
  <p>Why are they wasting the same amount of memory?
  Why did immutable String stop wasting memory?
  Off-topic: How can I convert <code>StringBuilder</code> to byte array encoded in a specific charset?</p>