Skip to content

Fix memory leak when preparing frames - #38

Draft
Avasam wants to merge 1 commit into
milankarman:mainfrom
Avasam:Fix-memory-leak-when-preparing-frames
Draft

Avasam wants to merge 1 commit into
milankarman:mainfrom
Avasam:Fix-memory-leak-when-preparing-frames

Conversation

@Avasam

@Avasam Avasam commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Bitmap frame = new(path); frame = CropImage(frame, crop); never disposed the full size bitmap. GDI+ pixels live in unmanaged memory the GC doesn't track, so 170k frames exhausted 32 GB of RAM plus Swap.

  • Add ImageProcessor.LoadCroppedImage, which disposes the source after cropping, and use it at every load site
  • Add ImageProcessor.LoadCroppedPhash, so frames are hashed and disposed one at a time, and pass digests to GetHashDictSimilarity
  • Dispose cropped bitmaps and the CommonOpenFileDialog
  • Enable CA2000 as an error (didn't catch the main issue here, but caught smaller ones)

Fixes #27


Keeping as a draft for now, because whilst this should work perfectly fine on Windows, I now hit the following two issues on Wine + X11:

Which make me go form "can't prepare frames on large videos (~40+ minutes at 1080p60)" to "can't prepare frames at all".

`Bitmap frame = new(path); frame = CropImage(frame, crop);` never disposed the full size bitmap. GDI+ pixels live in unmanaged memory the GC doesn't track, so 170k frames exhausted 32 GB of RAM plus Swap.

- Add `ImageProcessor.LoadCroppedImage`, which disposes the source after cropping, and use it at every load site
- Add `ImageProcessor.LoadCroppedPhash`, so frames are hashed and disposed one at a time, and pass digests to `GetHashDictSimilarity`
- Dispose cropped bitmaps and the `CommonOpenFileDialog`
- Enable CA2000 as an error (didn't catch the main issue here, but caught smaller ones)

Fixes milankarman#27
@milankarman

Copy link
Copy Markdown
Owner

I'm mentioning it here because it's somewhat related and will possibly conflict if I try to fix it now:
There's an issue with the delete project button if you try to delete a project that you had open in the same session.

  1. Open the project
  2. Hit close and return to start window
  3. Try deleting the project

You'll be met with an IO exception like:
System.IO.IOException: 'The process cannot access the file 'C:/...' because it is being used by another process.'

If this seems out of scope I can make a separate issue and or investigate myself later.

@Avasam

Avasam commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

I'm mentioning it here because it's somewhat
You'll be met with an IO exception like:
System.IO.IOException: 'The process cannot access the file 'C:/...' because it is being used by another process.'

Oh, unless Wine emulates this, which I doubt. It probably can't even happen on Linux, since files are not automatically locked by processes.

Don't worry about conflicts with any of my PRs, I'm proefficient enough in git to handle it :) Plus, I'm keeping this as draft for now. (in case https://gitlab.freedesktop.org/xorg/lib/libx11/-/merge_requests/316 gets merged sooner rather than later)

If you don't get to it before I do, I still have Windows machines to test. They're just not my daily driver.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Running out of memory during "Prepare Frames" step

2 participants