title: Cannot free memory once occupied by bytes.Buffer
body: <p>I receive bytes of compressed ASCII text in <code>compressedbytes</code> of type []byte. The problem I face is that the following procedure occupies a lot of memory that does not get freed after the function reaches its end and remains occupied during the whole runtime of the program.</p>
  
  <pre><code>    b := bytes.NewReader(compressedbytes)
      r, err := zlib.NewReader(b)
      if err != nil {
          panic(err)
      }
      cleartext, err = ioutil.ReadAll(r)
      if err != nil {
          panic(err)
      }
  </code></pre>
  
  <p>I noticed that the type in use is <code>bytes.Buffer</code> and this type has the <code>Reset()</code> and <code>Truncate()</code> functions but none of them allows to free the memory that is once occupied.</p>
  
  <p>The <a href="https://golang.org/pkg/bytes/#Buffer.Reset" rel="noreferrer">documentation</a> of <code>Reset()</code> states the following:</p>
  
  <blockquote>
    <p>Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the same as Truncate(0).</p>
  </blockquote>
  
  <p>How can I unset the buffer and free the memory again?
  My program needs about 50MB of memory during the run that takes 2h. When I import strings that are zlib compressed the program needs 200 MB of memory.</p>
  
  <p>Thanks for your help.</p>
  
  <p>=== Update</p>
  
  <p>I even created a separate function for the decompression and call the garbage collector manually with <code>runtime.GC()</code> after the program returns from that function without success. </p>
  
  <pre><code>// unpack decompresses zlib compressed bytes
  func unpack(packedData []byte) []byte {
      b := bytes.NewReader(packedData)
      r, err := zlib.NewReader(b)
      if err != nil {
          panic(err)
      }
      cleartext, err := ioutil.ReadAll(r)
      if err != nil {
          panic(err)
      }
      r.Close()
      return cleartext
  }
  </code></pre>