* (global): added --no-mem-optimization flag

This commit adds a new command-line argument that disables memory
optimizations AGH is using. These memory optimizations might be
necessary on low-memory devices, but they aren't free and there's a
performance hit (see #2044). Now they can be disabled - just pass
--no-mem-optimization when you run AGH or when you install the service
-- ./AdGuardHome -s install --no-mem-optimization

Closes: #2044
This commit is contained in:
Andrey Meshkov
2020-09-11 13:19:37 +03:00
parent cb8afde629
commit b3a68bb806
4 changed files with 60 additions and 32 deletions

31
main.go
View File

@@ -4,10 +4,6 @@
package main
import (
"os"
"runtime/debug"
"time"
"github.com/AdguardTeam/AdGuardHome/home"
)
@@ -21,32 +17,5 @@ var channel = "release"
var goarm = ""
func main() {
memoryUsage()
home.Main(version, channel, goarm)
}
// memoryUsage implements a couple of not really beautiful hacks which purpose is to
// make OS reclaim the memory freed by AdGuard Home as soon as possible.
func memoryUsage() {
debug.SetGCPercent(10)
// madvdontneed: setting madvdontneed=1 will use MADV_DONTNEED
// instead of MADV_FREE on Linux when returning memory to the
// kernel. This is less efficient, but causes RSS numbers to drop
// more quickly.
_ = os.Setenv("GODEBUG", "madvdontneed=1")
// periodically call "debug.FreeOSMemory" so
// that the OS could reclaim the free memory
go func() {
ticker := time.NewTicker(15 * time.Second)
for {
select {
case t := <-ticker.C:
t.Second()
debug.FreeOSMemory()
}
}
}()
}