Free up server memory space when idle

Asked

Viewed 119 times

0

I have a code sort of 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 you have in the file request.html and he performs in localhost:8080, see which routes I define in http.HandleFunc("/",taltal).

I could notice that in the task manager, even when the server was idle, the use of memory never decreased, only increased according to the number of calls I made to the server.

Initial state:

inserir a descrição da imagem aqui

After making some requests:

inserir a descrição da imagem aqui

And the use of memory never diminishes, even when the server is idle.

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

1 answer

1


When Go’s GC frees up memory, it doesn’t return it to OS, it becomes available for your program to reuse and only after a time as available, it is released to OS. In any case, you can use Freeosmemory to force this process (do not know how this in Windows, Linux works)

Freeosmemory forces a Garbage Collection Followed by an Attempt to Return as Much memory to the Operating system as possible. (Even if this is not called, the Runtime gradually Returns memory to the Operating system in a background task.)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.