title: Allocated a lot of memory in Go. How to fix?
body: <p>Several hundred MB of memory is allocated for 50 requests of 5 MB. Memory is allocated and is no longer released.
  How can I clear my memory? Why can this happen?</p>
  
  <p>I've tried on Ubuntu on my home pc and on VPS</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "fmt"
      "io/ioutil"
      "net/http"
      "time"
  )
  
  func main() {
      fmt.Println("start")
  
      for i := 0; i &lt; 50; i++ {
          go func() {
              DoRequest()
          }()
          time.Sleep(10 * time.Millisecond)
      }
  
      time.Sleep(10 * time.Minute)
  }
  
  func DoRequest() error {
      requestUrl := "https://blockchain.info/rawblock/0000000000000000000eebedea046425bd54626e6c56eb032e66e714d0141ea6"
  
      req, err := http.NewRequest("GET", requestUrl, nil)
  
      if err != nil {
          return err
      }
  
      req.Header.Set("user-agent", "free")
  
      httpClient := &amp;http.Client{
          Timeout: time.Second * 10,
      }
  
      resp, err := httpClient.Do(req)
  
      if resp != nil {
          defer resp.Body.Close()
      }
  
      if err != nil {
          return err
      }
  
      body, err := ioutil.ReadAll(resp.Body)
      if err != nil {
          return err
      }
  
      fmt.Println("bodylen", len(body))
  
      return nil
  }
  </code></pre>
  
  <p>Allocated somewhere 400MB</p>