Hopefully someone can clear this up. I am writing a recursive crawler, that when complete, returns a JSON string. At first, I thought it was my code in the master project, so I created a bare bones program to isolate and illustrate the issue.
When the below code is executed, it starts a server on port 8080, which delegates 1.1MB of RAM in my Activity Monitor. Everything ok so far..
When you curl that address, i.e. curl http://127.0.0.1:8080
, the handler executes by calling the given domain 2000 times. Once the go routines complete, it sends back a canned response. HOWEVER, the activity monitor now reads 29.9MB!? Shouldn't it go back down to 1.1MB because it is no longer operating? This has been driving me crazy, any help would be greatly appreciated.
Here's the dummy program:
package main
import (
"fmt"
"log"
"net/http"
"sync"
)
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
wg := &sync.WaitGroup{}
for i := 0; i < 2000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
r, err := http.Get("http://theswellcompany.com")
if err == nil {
fmt.Println(r.StatusCode)
err := r.Body.Close()
if err != nil {
// do nothing
}
}
}()
}
wg.Wait()
w.Header().Set("Content-Type", "test/html; charset=UTF-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string("DONE, but if it's done, WHY does the actvity monitor on my mac still read 40MB??"))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}