title: Memory leak in Go http standard library?
body: <p>Have a Go binary implement an http server:</p>
  
  <pre><code>package main
  
  import (
      "net/http"
  )
  
  func main() {
      http.ListenAndServe(":8080", nil)
  }
  </code></pre>
  
  <p>It will start with ~850 kb or so of memory. Send it some requests via your web browser. Observe it quickly rises to 1 mb. If you wait, you'll see it never goes down. Now hammer it with Apache Bench (using the script below) and see your memory usage continually increase. After sometime it will eventually plateau at around 8.2 MB or so.</p>
  
  <p>Edit: It doesn't seem to stop at 8.2, rather it slows down significantly. It's currently at 9.2 and still rising.</p>
  
  <p>In short, why is this happening? I used this shell script:</p>
  
  <pre><code>while [ true ]
  do
      ab -n 1000 -c 100 http://127.0.0.1:8080/
      sleep 1
  end
  </code></pre>
  
  <p>While trying to get to the bottom of this, I've tried to tweak the settings. I've tried closing using <code>r.Close = true</code> to prevent Keep-Alive. Nothing seems to work.</p>
  
  <p>I found this originally while trying to determine if there was a memory leak in a program I'm writing. It has a lot of http handlers and I/O calls. After checking I had closed all my database connections I kept seeing it's memory usage rise. My program to plateau at around <strong>433 MB</strong>.</p>
  
  <p>Here's the output of Goenv:</p>
  
  <pre><code>GOARCH="amd64"
  GOBIN=""
  GOCHAR="6"
  GOEXE=""
  GOHOSTARCH="amd64"
  GOHOSTOS="darwin"
  GOOS="darwin"
  GOPATH="/Users/mark/Documents/Programming/Go"
  GORACE=""
  GOROOT="/usr/local/go"
  GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
  TERM="dumb"
  CC="clang"
  GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fno-common"
  CXX="clang++"
  CGO_ENABLED="1"
  </code></pre>