mirror of
https://github.com/diamondburned/cchat-gtk.git
synced 2025-01-23 18:36:40 +00:00
Enforce aggressive GC for Linux
This commit is contained in:
parent
da86930590
commit
ba4728c3d6
47
madvdontneed.go
Normal file
47
madvdontneed.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
// +build linux
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Inject madvdontneed=1 as soon as possible.
|
||||
|
||||
var _ = func() struct{} {
|
||||
// If GODEBUG is not empty, then use that instead of trying to exec our own.
|
||||
if os.Getenv("GODEBUG") != "" {
|
||||
return struct{}{}
|
||||
}
|
||||
|
||||
// Do magic.
|
||||
var environ = append(os.Environ(), "GODEBUG=madvdontneed=1")
|
||||
|
||||
log.Println("execve(2)ing with madvdontneed=1 for aggressive GC.")
|
||||
|
||||
if err := syscall.Exec(os.Args[0], os.Args, environ); err != nil {
|
||||
log.Fatalln("Fatal error while executing:", err)
|
||||
} else {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
return struct{}{}
|
||||
}()
|
||||
|
||||
func init() {
|
||||
// Aggressive memory freeing you asked, so aggressive memory freeing we will
|
||||
// deliver.
|
||||
if strings.Contains(os.Getenv("GODEBUG"), "madvdontneed=1") {
|
||||
go func() {
|
||||
log.Println("Now attempting to free memory every 5s... (madvdontneed=1)")
|
||||
for range time.Tick(5 * time.Second) {
|
||||
debug.FreeOSMemory()
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
18
main.go
18
main.go
|
@ -1,11 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/diamondburned/cchat-gtk/internal/gts"
|
||||
"github.com/diamondburned/cchat-gtk/internal/log"
|
||||
"github.com/diamondburned/cchat-gtk/internal/ui"
|
||||
|
@ -19,19 +14,6 @@ import (
|
|||
// destructor is used for debugging and profiling.
|
||||
var destructor = func() {}
|
||||
|
||||
func init() {
|
||||
// Aggressive memory freeing you asked, so aggressive memory freeing we will
|
||||
// deliver.
|
||||
if strings.Contains(os.Getenv("GODEBUG"), "madvdontneed=1") {
|
||||
go func() {
|
||||
log.Println("Now attempting to free memory every 5s... (madvdontneed=1)")
|
||||
for range time.Tick(5 * time.Second) {
|
||||
debug.FreeOSMemory()
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
gts.Main(func() gts.MainApplication {
|
||||
var app = ui.NewApplication()
|
||||
|
|
Loading…
Reference in a new issue