This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
FROM golang:1.15-alpine AS builder
ENV CGO_ENABLED=0
WORKDIR /src
COPY ./src/go.mod .
RUN go mod download
COPY ./src/main.go .
RUN go build -o /server
# app
FROM alpine:3.15
EXPOSE 80
CMD ["/app/server"]
WORKDIR /app
COPY ./src/config.toml .
COPY --from=builder /server .

View File

@@ -0,0 +1,2 @@
interval = 5
allocation = 10

View File

@@ -0,0 +1,7 @@
module kiamol/memory-allocator
go 1.15
require (
github.com/spf13/viper v1.10.1
)

View File

@@ -0,0 +1,47 @@
package main
import (
"fmt"
"time"
"github.com/spf13/viper"
)
type Configuration struct {
Interval int
Allocation int
}
func getConfig() Configuration {
viper.SetEnvPrefix("KIAMOL")
viper.AutomaticEnv()
viper.SetConfigFile("./config.toml")
viper.ReadInConfig()
config := Configuration{}
viper.Unmarshal(&config)
return config
}
// adapted from:
// https://golangcode.com/print-the-current-memory-usage/
func main() {
config := getConfig()
var overall [][]int
//4.2M ints is about 1MB physical allocation:
length := config.Allocation * 4200000
count := 1
for {
// Allocate memory using make() and append to overall (so it doesn't get
// garbage collected). This is to create an ever increasing memory usage
// which we can track. We're just using []int as an example.
a := make([]int, 0, length)
overall = append(overall, a)
fmt.Printf("Allocated ~%vMiB\n", count * config.Allocation)
time.Sleep(time.Duration(config.Interval) * time.Second)
count++
}
}