package main
import (
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"time"
)
var holder = make([][]byte, 0)
func main() {
blockSize := 100 * 1024 * 1024
printInterval := time.Second
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
ticker := time.NewTicker(printInterval)
defer ticker.Stop()
var totalAllocated uint64
for {
select {
case <-ticker.C:
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("Allocated: %.2f GB\n", float64(totalAllocated)/1024/1024/1024)
case sig := <-sigChan:
fmt.Printf("Received signal: %v\n", sig)
return
default:
buf := make([]byte, blockSize)
time.Sleep(100 * time.Second)
for i := range buf {
buf[i] = byte(i % 256)
}
holder = append(holder, buf)
totalAllocated += uint64(blockSize)
}
}
}
I executed a piece of go code, constantly applying for memory, memory ran out, the system was stuck, the process was not killed。
I have tried programming languages such as php, node.js, python, and java. When memory runs out, rocky9.5 may freeze. So what should I do? In the go code, I applied for memory and deliberately slept for 100 seconds, but still got stuck in the end。
This link is the topic I posted before.