I have created a scheduler which run every 10 seconds using golang newTicker. Each tick creates a new goroutine which performs some memory intensive task but finishes well before 10 seconds are completed.
I have deployed this in kubernetes. container has both scheduler and http server. HTTP server will take a single request and run the schedule once. This is for retrying missed tasked manually. Code is as follows:
func startScheduledTask() {
fmt.Println("Task Started...", time.Now())
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for ; true; <-ticker.C {
go customTask(time.Now())
}
}
I observed that this code is not releasing memory. Docker stats for container shows the memory is growing. Though it has memory limit on k8s, and k8s will restart in case of OOMKilled, one scheduled tasked is still missed and manual intervention is required. Is there a way to release this memory?
customTask Code is making HTTP Request for each tick.
Docker stats and container logs are as follows.