[bug] Application is slowly leaking memory/taking to much time to garbage collect
What happend?
I have an application which visualizes 4 matrices ,which get updated a lot, around 50 times a second.
When I observer the application in the task manager, sometimes it runs with 200mb of memory continuously, but on other occasions, it will just eat all the 64 GB of RAM that I have, making the system quite unresponsive.
When I profile the application, all of them RAM usage is invisible to me, so I am suspecting that its some internal imgui handling with the textures that do not get freed, or don't get freed fast enough.
I have attached a code sample. It sometimes works for me. You can see it mostly sitting around 100MB, but it sometimes goes up to 500MB.
Again, in the real application I render much more data, so the issue occurs more often.
I tried to find a way to manually "release" the image, but this functionality is not exposed.
Any ideas or mitigations would be greatly appreciated.
Best Regards
Code example
main.go
package main
import (
"crypto/rand"
"image"
"image/color"
"github.com/AllenDang/giu"
)
var texture *giu.Texture
func loop() {
giu.Window("window 1").Layout(
giu.Image(texture).Size(512, 512),
)
}
func RenderImage(img *image.RGBA) {
buf := make([]byte, 3)
for x := 0; x < 256; x++ {
for y := 0; y < 256; y++ {
rand.Read(buf)
img.Set(x, y^255, color.NRGBA{
R: uint8(buf[0]), G: uint8(buf[1]),
B: uint8(buf[2]), A: 255,
})
}
}
}
func main() {
wnd := giu.NewMasterWindow("windows [DEMO]", 640, 480, 0)
go func() {
img := image.NewRGBA(image.Rect(0, 0, 256, 256))
for {
RenderImage(img)
giu.NewTextureFromRgba(img, func(tex *giu.Texture) {
/* we have to find a way to release the texture here */
texture = tex /* do we leak here ?*/
})
giu.Update()
}
}()
wnd.Run(loop)
}
To Reproduce
- Run my demo
- See memory usage spike sometimes
- If the spike rises fast enough, the system will start swapping and memory usage just starts to climb without going down until I kill the application
Version
v0.6.2
OS
Windows 10
It seems the Finalizer doesn't guarantee to work. You could create a texture from image and release it manually to see whether the problem is solved.
texId, err := giu.Context.GetRenderer().LoadImage(...)
giu.Context.GetRenderer().ReleaseImage(...)
Thanks for the quick reply.
I am uncertain if I am holding it wrong but the following code
Code example
main.go
package main
import (
"crypto/rand"
"image"
"image/color"
"log"
"github.com/AllenDang/giu"
"github.com/AllenDang/imgui-go"
)
var texture *giu.Texture
var textureId imgui.TextureID
func loop() {
giu.Window("window 1").Layout(
giu.Image(texture).Size(512, 512),
)
}
func RenderImage(img *image.RGBA) {
buf := make([]byte, 3)
for x := 0; x < 256; x++ {
for y := 0; y < 256; y++ {
rand.Read(buf)
img.Set(x, y^255, color.NRGBA{
R: uint8(buf[0]), G: uint8(buf[1]),
B: uint8(buf[2]), A: 255,
})
}
}
}
func main() {
wnd := giu.NewMasterWindow("windows [DEMO]", 640, 480, 0)
go func() {
img := image.NewRGBA(image.Rect(0, 0, 256, 256))
for {
RenderImage(img)
// textureId, err := giu.Context.GetRenderer().LoadImage(img)
_, err := giu.Context.GetRenderer().LoadImage(img)
if err != nil {
log.Fatalf("loadImage failed: %v", err)
}
// texture = giu.ToTexture(textureId)
giu.Update()
}
}()
wnd.Run(loop)
}
leads to a
Exception 0xc0000005 0x0 0x1408 0x7ffba4c4dcd9 PC=0x7ffba4c4dcd9 signal arrived during external code execution
on line
_, err := giu.Context.GetRenderer().LoadImage(img)
and when stepping down into the function it appear that
gl.GenerateMipmap(gl.TEXTURE_2D)
is the line crashing
For me it works now so I suppose this was fixed. Let me know if you still have this problem.