Free up server memory space when idle

0

I have a code more or less with this structure:

package main

import (
    "html/template"
    "net/http"
    "log"
)

func main() {
    http.HandleFunc("/",taltal)
    http.ListenAndServe(":8080",nil)
}

func check(err error){
    if err != nil{
        log.Fatal(err)
    }
}

func taltal(w http.ResponseWriter, r *http.Request){
    if r.Method == "GET"{
        t,err := template.ParseFiles("request.html")
        check(err)
        t.Execute(w,nil)
    }
}

This code simply creates a server with whatever it has in the file request.html and it runs in localhost:8080 , see the routes I set in http.HandleFunc("/",taltal) .

I noticed that in the task manager, even when the server was idle, memory usage never decreased, it only increased according to the amount of calls I made to the server.

Initial status:

Aftermakingsomerequests:

And memory usage never slows, even when the server is idle.

How do I free the memory that is no longer being used and at the same time do not crash the server?

    
asked by anonymous 26.03.2017 / 05:00

1 answer

1

When Go's GC frees memory, it does not mean that it returns it to the OS, that memory is available for your program to reuse, and only after a time as available, it is released to the OS. In any case, you can use FreeOSMemory to force this process (I do not know how it works in Windows, Linux works )

  

FreeOSMemory forces garbage collection followed by an attempt to   return as much memory as possible. (Even if   this is not called, the runtime gradually returns memory to the   operating system in a background task.)

    
04.04.2017 / 16:37