henrybetts / swift-webgpu

Swift bindings for WebGPU
MIT License
78 stars 3 forks source link

RAM usage climbs higher and higher for rendering demos #3

Closed stackotter closed 2 years ago

stackotter commented 2 years ago

Overview

I was running DemoCube for a little while and the memory usage climbed to over 5gb. Even the clear colour demo has this issue. RAM usage seems to climb slower for the triangle and clear colour demos. The cube demo memory usage climbs by about 8mb each second and the other two rise by about 4mb each second.

The number of ports also rises very quickly (can be seen in activity monitor). I don't know what would cause the number of mach ports to rise like that, but it could be related to the issue.

Leaving the demo open while using other parts of my laptop causes my laptop to frequently freeze up (even when the RAM usage isn't ridiculously high yet).

This could well be an issue with Dawn unless there is some kind of retain cycle in swift-webgpu.

Reproducing

Build dawn (2aa0042b) and the swift-webgpu demos (c1f91b2) using the instructions in the readme. Run the demo from terminal with the command .build/release/DemoCube. Observe the RAM usage climbing in activity monitor.

System

henrybetts commented 2 years ago

I think this is specific to macOS, and probably because there are no autorelease pools being used in the demos.

I thought I saw a Dawn bug report a while ago which suggested they were going to add an autorelease pool internally within Dawn, so I was just waiting on that. But it looks like they have added autorelease pools to the Dawn examples since I last checked, so might be worth doing the same to the swift-webgpu demos.

The number of ports rising sounds odd, but could be related if resources aren't being freed.

stackotter commented 2 years ago

Yep! That was the issue, thanks :) It also fixed the number of ports issue. I just nested the autoreleasepool inside of the repeat loop. I'll make a PR that fixes it once I get time this afternoon (unless you get to it first of course).

henrybetts commented 2 years ago

No worries - already taken care of! 5f340db

stackotter commented 2 years ago

Thanks! 👍