TomHarte / CLK

A latency-hating emulator of: the Acorn Electron and Archimedes, Amstrad CPC, Apple II/II+/IIe and early Macintosh, Atari 2600 and ST, ColecoVision, Enterprise 64/128, Commodore Vic-20 and Amiga, MSX 1/2, Oric 1/Atmos, early PC compatibles, Sega Master System, Sinclair ZX80/81 and ZX Spectrum.
MIT License
911 stars 50 forks source link

Memory leaks #1311

Open ryandesign opened 5 months ago

ryandesign commented 5 months ago

In the macOS Metal version at least, when an emulated machine is running, Clock Signal uses more memory the longer it is open. Please check for memory leaks. For example, in Xcode, select Profile from the Product menu and then choose the Leaks template.

I'm on macOS 14.2.1 today, on my 2012 15" MacBook Pro with Retina display, using the code from git as of yesterday (3ee81efe404d7773b403adfa7f5d83b07e18de87). I'm using an Apple IIe emulation. The problem happens regardless of whether I'm using the integrated or discrete GPU. The memory usage increases more slowly if the frame rate is lower (e.g. because of #1178).

ryandesign commented 5 months ago

I'm not familiar with using the Xcode/Instruments Leaks tool. I think it is supposed to detect leaks, but for me it only seems to detect a couple small leaks early in the run of the program, not the ever-increasing memory use while an emulator is running. I think it may be because these are Objective-C objects rather than plain C allocations. It does show an increasing number of allocations of particular sizes, though, and you can drill down and see which lines of your code caused those allocations. Maybe a corresponding release is missing or an autorelease pool needs to be added.

ryandesign commented 4 months ago

I should add that the memory leak is fairly significant in that it adds up pretty quickly. For example, after leaving an Apple IIe emulator running for maybe half an hour playing a few games of Tetris, Clock Signal is using over 20GB of memory. Closing the emulator window then takes quite a bit of time, with a spinning beachball cursor, while the OS frees all that memory.

The problem doesn't happen in the SDL/OpenGL build on the same Mac.

TomHarte commented 4 months ago

Noting that I'm unable to reproduce; there is a leak in Apple's Metal analysis tooling which affects runs in Instruments but outside of that:

Screenshot 2024-03-02 at 21 29 57

i.e. after an hour and ten minutes of CPU time, which at around 19% CPU utilisation is a bit more than six hours of wall clock time, the application is still using only around 90mb of 'real' memory, which even per the dedicated memory tab is less than 300mb:

Screenshot 2024-03-02 at 21 30 23