Skip to content

Memory Leak during continuous image updates #30

Open
@hungry

Description

Hello!

I have encountered a memory leak issue when using the Spot library to develop a program that functions similarly to the magnifier tool in Windows. The program continuously captures a portion of the screen, magnifies it, and displays it in a window. However, over time, the memory usage of the program keeps increasing, leading to a memory leak.

Code Snippet

package main

import (
	"image"
	"image/color"
	"log"
	"math/rand"
	"runtime"
	"time"

	"github.com/roblillack/spot"
	"github.com/roblillack/spot/ui"
)

func logMemoryUsage() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	log.Printf("TotalAlloc = %v MiB", m.TotalAlloc/1024/1024)
}

func main() {
	width, height := 5, 5
	zoom := 100

	ui.Init()

	spot.MountFn(func(ctx *spot.RenderContext) spot.Component {
		canvas, SetCanvas := spot.UseState(ctx, image.NewRGBA(image.Rect(0, 0, width*zoom, height*zoom)))

		spot.UseEffect(ctx, func() {
			go func() {
				i := 0
				for {
					for x := 0; x < width; x++ {
						for y := 0; y < height; y++ {
							g := uint8(rand.Intn(256))
							color := color.RGBA{g, g, g, 255}
							for zx := 0; zx < zoom; zx++ {
								for zy := 0; zy < zoom; zy++ {
									canvas.Set(x*zoom+zx, y*zoom+zy, color)
								}
							}
						}
					}
					SetCanvas(canvas)

					if i%10 == 0 {
						logMemoryUsage()
					}
					i += 1

					time.Sleep(300 * time.Millisecond)
				}
			}()
		}, []any{})

		return &ui.Window{
			Title:  "Title",
			Width:  canvas.Rect.Dx(),
			Height: canvas.Rect.Dy(),
			Children: []spot.Component{
				&ui.Image{
					X: 0, Y: 0,
					Width:  canvas.Rect.Dx(),
					Height: canvas.Rect.Dy(),
					Image:  canvas,
				},
			},
		}
	})

	ui.Run()
}

Invocation
go build -ldflags "-s -w" && main.exe

Program output

2024/12/06 12:53:20 TotalAlloc = 2 MiB
2024/12/06 12:53:23 TotalAlloc = 22 MiB
2024/12/06 12:53:26 TotalAlloc = 41 MiB
2024/12/06 12:53:29 TotalAlloc = 60 MiB
2024/12/06 12:53:32 TotalAlloc = 79 MiB
2024/12/06 12:53:35 TotalAlloc = 99 MiB
2024/12/06 12:53:39 TotalAlloc = 118 MiB
2024/12/06 12:53:42 TotalAlloc = 137 MiB
2024/12/06 12:53:45 TotalAlloc = 156 MiB
2024/12/06 12:53:48 TotalAlloc = 175 MiB
2024/12/06 12:53:51 TotalAlloc = 194 MiB
2024/12/06 12:53:55 TotalAlloc = 214 MiB
2024/12/06 12:53:58 TotalAlloc = 233 MiB
2024/12/06 12:54:01 TotalAlloc = 252 MiB
2024/12/06 12:54:04 TotalAlloc = 271 MiB
2024/12/06 12:54:07 TotalAlloc = 290 MiB
2024/12/06 12:54:11 TotalAlloc = 309 MiB
2024/12/06 12:54:14 TotalAlloc = 328 MiB
2024/12/06 12:54:17 TotalAlloc = 348 MiB
2024/12/06 12:54:20 TotalAlloc = 367 MiB

Expected Behavior
The program should continuously update the displayed image without a significant increase in memory usage.

Actual Behavior
The program's memory usage keeps increasing over time, indicating a memory leak.

Additional Information
Go version: go1.22.5 windows/amd64
Spot library version: v0.3.2
Operating System: Windows 10

I would appreciate any guidance or fixes to resolve this memory leak issue. Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions