FAQ
Hi

Do you always need to close the HTTP response body? Even if the response is
never read or even used?

This code reports memory usage of around 30MB on OSX:
package main

import (
"log"
"net/http"
"net/http/httptest"
"runtime"
)

func main() {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter,
r *http.Request) {
rw.Write([]byte("Hello World"))
}))
defer server.Close()

for i := 0; i < 1000; i++ {
_, err := http.Get(server.URL)
if err != nil {
log.Println(err)
return
}
}

memStats := new(runtime.MemStats)
runtime.ReadMemStats(memStats)
log.Println(memStats.HeapAlloc)
}


However, this code only uses around 2MB:

package main

import (
"log"
"net/http"
"net/http/httptest"
"runtime"
)

func main() {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter,
r *http.Request) {
rw.Write([]byte("Hello World"))
}))
defer server.Close()

for i := 0; i < 1000; i++ {
resp, err := http.Get(server.URL)
if err != nil {
log.Println(err)
return
}
resp.Body.Close()
}

memStats := new(runtime.MemStats)
runtime.ReadMemStats(memStats)
log.Println(memStats.HeapAlloc)
}


The only difference is that in the second snippet I'm closing the response
body.

Am I missing something?

Thanks
Ryan



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ryan Slade at Jun 13, 2013 at 3:43 pm
    That's using Go 1.1.1
    On Thursday, 13 June 2013 16:39:50 UTC+1, Ryan Slade wrote:

    Hi

    Do you always need to close the HTTP response body? Even if the response
    is never read or even used?

    This code reports memory usage of around 30MB on OSX:
    package main

    import (
    "log"
    "net/http"
    "net/http/httptest"
    "runtime"
    )

    func main() {
    server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter,
    r *http.Request) {
    rw.Write([]byte("Hello World"))
    }))
    defer server.Close()

    for i := 0; i < 1000; i++ {
    _, err := http.Get(server.URL)
    if err != nil {
    log.Println(err)
    return
    }
    }

    memStats := new(runtime.MemStats)
    runtime.ReadMemStats(memStats)
    log.Println(memStats.HeapAlloc)
    }


    However, this code only uses around 2MB:

    package main

    import (
    "log"
    "net/http"
    "net/http/httptest"
    "runtime"
    )

    func main() {
    server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter,
    r *http.Request) {
    rw.Write([]byte("Hello World"))
    }))
    defer server.Close()

    for i := 0; i < 1000; i++ {
    resp, err := http.Get(server.URL)
    if err != nil {
    log.Println(err)
    return
    }
    resp.Body.Close()
    }

    memStats := new(runtime.MemStats)
    runtime.ReadMemStats(memStats)
    log.Println(memStats.HeapAlloc)
    }


    The only difference is that in the second snippet I'm closing the response
    body.

    Am I missing something?

    Thanks
    Ryan


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tamás Gulácsi at Jun 13, 2013 at 4:35 pm
    AFAIK yes.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dominik Honnef at Jun 13, 2013 at 4:46 pm

    Ryan Slade writes:

    Hi

    Do you always need to close the HTTP response body? Even if the response is
    never read or even used?

    This code reports memory usage of around 30MB on OSX:
    package main
    If the body is not nil, i.e. there was no error, you need to close it.
    The documentation says to close it when done using it. That does not
    imply that you have to first read, or otherwise work with it. If you
    have a body, close it once you don't need it anymore.

    Otherwise it cannot be released and you will leak memory.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Cheney at Jun 14, 2013 at 3:16 am
    You are correct. You MUST close the response body. It says so in the documentation for net/http.
    On 14/06/2013, at 1:39, Ryan Slade wrote:

    Hi

    Do you always need to close the HTTP response body? Even if the response is never read or even used?

    This code reports memory usage of around 30MB on OSX:
    package main

    import (
    "log"
    "net/http"
    "net/http/httptest"
    "runtime"
    )

    func main() {
    server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    rw.Write([]byte("Hello World"))
    }))
    defer server.Close()

    for i := 0; i < 1000; i++ {
    _, err := http.Get(server.URL)
    if err != nil {
    log.Println(err)
    return
    }
    }

    memStats := new(runtime.MemStats)
    runtime.ReadMemStats(memStats)
    log.Println(memStats.HeapAlloc)
    }


    However, this code only uses around 2MB:

    package main

    import (
    "log"
    "net/http"
    "net/http/httptest"
    "runtime"
    )

    func main() {
    server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    rw.Write([]byte("Hello World"))
    }))
    defer server.Close()

    for i := 0; i < 1000; i++ {
    resp, err := http.Get(server.URL)
    if err != nil {
    log.Println(err)
    return
    }
    resp.Body.Close()
    }

    memStats := new(runtime.MemStats)
    runtime.ReadMemStats(memStats)
    log.Println(memStats.HeapAlloc)
    }


    The only difference is that in the second snippet I'm closing the response body.

    Am I missing something?

    Thanks
    Ryan



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at Jun 14, 2013 at 3:24 am

    On Fri, Jun 14, 2013 at 1:39 AM, Ryan Slade wrote:

    Do you always need to close the HTTP response body? Even if the response
    is never read or even used?
    Why make a http request if you're not interested in the response?

    --
    =====================
    http://jessta.id.au

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Gustavo Niemeyer at Jun 14, 2013 at 4:23 am

    On Fri, Jun 14, 2013 at 12:24 AM, Jesse McNelis wrote:
    Why make a http request if you're not interested in the response?
    Headers are enough surprisingly often.


    gustavo @ http://niemeyer.net

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • David Symonds at Jun 14, 2013 at 4:33 am

    On 14 June 2013 14:23, Gustavo Niemeyer wrote:
    On Fri, Jun 14, 2013 at 12:24 AM, Jesse McNelis wrote:
    Why make a http request if you're not interested in the response?
    Headers are enough surprisingly often.
    Or if it's just a POST and you just care that it succeeds.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Voidlogic7 at Jun 15, 2013 at 2:20 pm
    Then don't use http.Get, use http.Head

    http://golang.org/pkg/net/http/#Head
    See sec 9.4: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
    On Thursday, June 13, 2013 11:23:17 PM UTC-5, Gustavo Niemeyer wrote:
    On Fri, Jun 14, 2013 at 12:24 AM, Jesse McNelis wrote:
    Why make a http request if you're not interested in the response?
    Headers are enough surprisingly often.


    gustavo @ http://niemeyer.net
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Francisco Souza at Jun 16, 2013 at 2:19 am

    On Sat, Jun 15, 2013 at 11:20 AM, wrote:
    Then don't use http.Get, use http.Head
    Sometimes you want to issue a GET request and handle only the head.
    It's not the same as issuing a HEAD request. It should be, but it's
    not.



    --
    ~f

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 13, '13 at 3:39p
activeJun 16, '13 at 2:19a
posts10
users9
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase