-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (59 loc) · 1.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"bytes"
"log"
"os"
"os/exec"
"strconv"
"time"
"github.com/shirou/gopsutil/mem"
)
func ramToConsume() []byte {
v, err := mem.VirtualMemory()
if err != nil {
log.Println("Error retrieving virtual memory info:", err)
return nil
}
takeEnv := os.Getenv("TAKE")
take := 15
if takeEnv != "" {
take, err = strconv.Atoi(takeEnv)
if err != nil {
log.Println("Error parsing TAKE environment variable:", err)
take = 15
}
}
memoryToAllocate := float64(v.Total)*float64(take)/100.0 - float64(v.Used)
if memoryToAllocate > 0 {
allocatedMem := bytes.Repeat([]byte{0}, int(memoryToAllocate))
log.Printf("Allocated %d bytes to reach target memory usage.\n", len(allocatedMem))
return allocatedMem
} else {
log.Println("No need to allocate memory.")
return nil
}
}
func main() {
_ = ramToConsume()
log.Println("Done!")
for {
time.Sleep(24 * time.Hour)
if os.Getenv("NOCPUB") == "" {
result := 1
for i := 1; i < 1000000; i++ {
result *= i
}
}
args := []string{"main.go"}
if _, err := os.Stat("main.go"); os.IsNotExist(err) {
args[0] = "main"
}
cmd := exec.Command(os.Args[0], args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
if err := cmd.Run(); err != nil {
log.Println("Error re-executing the program:", err)
}
}
}