Original post

I am trying to create a MicroService using POST … doing some Memory Tests it seems that there is a Leak … but I am not getting why …

here is the Code I simply reply the transferred data back to Curl.

package main

import (
        "fmt"
        "io/ioutil"
        "net/http"
        "log"
        "runtime"
)

func check(e error) {
        if e != nil {
                panic(e)
        }
}

/**
  * @brief Print Mem Useage For info on each, see: https://.org/pkg/runtime/#MemStats
        */
func PrintMemUsage() {
        var m runtime.MemStats
        runtime.ReadMemStats(&m)
            fmt.Printf("t(Mallocs=%v - Frees=%v) = %v",  (m.Mallocs), (m.Frees), (m.Mallocs)-(m.Frees))
            fmt.Printf("tHeapAlloc=%v  ",  (m.HeapAlloc))
            fmt.Printf("tHeapSys=%v  ",  (m.HeapSys))
            fmt.Printf("tHeapObjects=%v  ",  (m.HeapObjects))
        fmt.Printf("tSys=%v n",  (m.Sys))
}

/**
 * @brief Respond with File sent
 * call me via curl -X POST -F mwFile=@<Path to a 150MB File> http://localhost:8077/leakTest > OUTFile
 */
func leakTest(w http.ResponseWriter, r *http.Request) {
    // parses request body as multipart/form-data up to maxMemory bytes (200000000)
        r.ParseMultipartForm(200000000)

        //  returns the first file for the provided form key "mwFile"
        file, _, err := r.FormFile("mwFile")
        check(err)

        // read all of the contents of our uploaded file into a byte array
        fileBytes, err := ioutil.ReadAll(file)
        check(err)

    file.Close()

        // later we need this String .. but ot for now ...
        ioStr := string((fileBytes))

    // Reply to CURL
        fmt.Fprintf(w,ioStr)

        PrintMemUsage()

}

func setupRoutes() {
        http.HandleFunc("/leakTest", leakTest)
        log.Fatal(http.ListenAndServe(":8077", nil))
}

func main() {
        setupRoutes()
}

To test the MicroService I use a Shell Script (tests.sh) like

# $1, the Filename
# $2, the amount of tests to do
doTheTwist(){
  for ((i=1;i<=$2;i++))
    do
      fname=$1
       echo Test $i and $fname
      curl  -s -o test.out -X POST -H 'Expect:' -F "mwFile=@$fname"  http://localhost:8077/leakTest
    done
}

doTheTwist $1 $2

Calling .

./tests.sh some/file/about/17MB.json 1000

Running the MicroService and annoying it with the Test-Script shows that the HeapObjects are growing overtime … but I am not getting why … somethimes they shrink again … a bit … but over time they grow more and more … shouldn’t the HeapObjects stay more or less the same size ?

(Mallocs=120830 - Frees=119807) = 1023       HeapAlloc=600646832     HeapSys=1341227008      HeapObjects=1023        Sys=1390729624 
(Mallocs=120984 - Frees=119962) = 1022  HeapAlloc=600646816     HeapSys=1341227008      HeapObjects=1022        Sys=1390729624 
(Mallocs=121144 - Frees=120119) = 1025  HeapAlloc=600647024     HeapSys=1341227008      HeapObjects=1025        Sys=1390729624 
(Mallocs=121297 - Frees=120272) = 1025  HeapAlloc=600647104     HeapSys=1341227008      HeapObjects=1025        Sys=1390729624 
(Mallocs=121455 - Frees=120429) = 1026  HeapAlloc=600647120     HeapSys=1341227008      HeapObjects=1026        Sys=1390729624 
(Mallocs=121608 - Frees=120581) = 1027  HeapAlloc=600647504     HeapSys=1341227008      HeapObjects=1027        Sys=1390729624 
(Mallocs=121766 - Frees=120739) = 1027  HeapAlloc=600647504     HeapSys=1341227008      HeapObjects=1027        Sys=1390729624 
(Mallocs=121918 - Frees=120891) = 1027  HeapAlloc=600647504     HeapSys=1341227008      HeapObjects=1027        Sys=1390729624 
(Mallocs=122071 - Frees=121044) = 1027  HeapAlloc=600647872     HeapSys=1341227008      HeapObjects=1027        Sys=1390729624 
(Mallocs=122227 - Frees=121199) = 1028  HeapAlloc=600647888     HeapSys=1341227008      HeapObjects=1028        Sys=1390729624 
(Mallocs=122383 - Frees=121355) = 1028  HeapAlloc=600647888     HeapSys=1341227008      HeapObjects=1028        Sys=1390729624 
(Mallocs=122537 - Frees=121510) = 1027  HeapAlloc=600647872     HeapSys=1341227008      HeapObjects=1027        Sys=1390729624 
(Mallocs=122691 - Frees=121663) = 1028  HeapAlloc=600647888     HeapSys=1341227008      HeapObjects=1028        Sys=1390729624 
(Mallocs=122848 - Frees=121819) = 1029  HeapAlloc=600648272     HeapSys=1341227008      HeapObjects=1029        Sys=1390729624 
(Mallocs=123009 - Frees=121977) = 1032  HeapAlloc=600649136     HeapSys=1341227008      HeapObjects=1032        Sys=1390729624 
(Mallocs=123165 - Frees=122134) = 1031  HeapAlloc=600649120     HeapSys=1341227008      HeapObjects=1031        Sys=1390729624 
(Mallocs=123324 - Frees=122291) = 1033  HeapAlloc=600649520     HeapSys=1341227008      HeapObjects=1033        Sys=1390729624 
(Mallocs=123483 - Frees=122449) = 1034  HeapAlloc=600649904     HeapSys=1341227008      HeapObjects=1034        Sys=1390729624 
(Mallocs=123640 - Frees=122606) = 1034  HeapAlloc=600650272     HeapSys=1341227008      HeapObjects=1034        Sys=1390729624 
(Mallocs=123795 - Frees=122759) = 1036  HeapAlloc=600650672     HeapSys=1341227008      HeapObjects=1036        Sys=1390729624 
(Mallocs=123953 - Frees=122917) = 1036  HeapAlloc=600650672     HeapSys=1341227008      HeapObjects=1036        Sys=1390729624 
(Mallocs=124103 - Frees=123068) = 1035  HeapAlloc=600650656     HeapSys=1341227008      HeapObjects=1035        Sys=1390729624