title: Zero-garbage large String deserialization in Java, Humongous object issue
body: <p>I am looking for a way to deserialize a <code>String</code> from a <code>byte[]</code> in Java with as little garbage produced as possible. Because I am creating my own serializer and de-serializer, I have complete freedom to implement any solution on the server-side (i.e. when serializing data), and on the client-side (i.e. when de-serializing data).</p>
  
  <p>I have managed to efficiently <strong>serialize</strong> a <code>String</code> without incurring any garbage overhead by iterating through the <code>String's</code> chars (<code>String.charAt(i)</code>) and converting each <code>char</code> (16-bit value) to 2x 8-bit value. There is a nice debate regarding this <a href="https://stackoverflow.com/questions/8894258/fastest-way-to-iterate-over-all-the-chars-in-a-string">here</a>. An alternative is to use Reflection to access <code>String's</code> underlying <code>char[]</code> directly, but this in outside the scope of the problem.</p>
  
  <p>However, it seems impossible for me to deserialize the <code>byte[]</code> without creating the <code>char[]</code> <strong>twice</strong>, which seems, well, weird.</p>
  
  <p>The procedure:</p>
  
  <ol>
  <li>Create <code>char[]</code></li>
  <li>Iterate through <code>byte[]</code> and fill-in the <code>char[]</code></li>
  <li>Create String with <code>String(char[])</code> constructor</li>
  </ol>
  
  <p>Because of Java's <code>String</code> immutability rules, the constructor copies the char[], creating 2x GC overhead. I can always use mechanisms to circumvent this (Unsafe <code>String</code> allocation + Reflection to set the <code>char[]</code> instance), but I just wanted to ask if there are any consequences to this other than me breaking every convention on <code>String's</code> immutability.</p>
  
  <p>Of course, the wisest response to this would be "come on, stop doing this and have trust in GC, the original <code>char[]</code> will be extremely short-lived and G1 will get rid of it momentarily", which actually makes sense, <strong>if the <code>char[]</code> is smaller than 1/2 of the G1's region size</strong>. If it is larger, the char[] will be directly allocated as a humongous object (i.e. automatically propagated outside of the G1's region). Such objects are extremely hard to be efficiently garbage collected in G1. That's why each allocation matters.</p>
  
  <p>Any ideas on how to tackle the issue?</p>
  
  <p>Many thanks.</p>