C
C#2y ago
Starlk

Optimizing Memory Usage

I've a tool that basically takes a bunch of files and converts them to a GIF. The way it works is that the files it reads are in binary and each byte represents a color, I start by reading the whole file then create a SKBitmap and SKCanvas to draw each pixel, then I add it to a MagickImageCollection that finally writes everything to a GIF, the tool works perfectly fine but I kind of hate of it uses almost 200 MB of RAM just with 40 files. https://pastebin.com/PPivEW9P
17 Replies
canton7
canton72y ago
Directory.GetFiles("Backups") -- that doesn't return the results in any guaranteed order
Starlk
Starlk2y ago
It apparently starts from the top to bottom
canton7
canton72y ago
The order is not guaranteed. The order probably comes from the filesystem itself, which IIRC is usually file creation order, but not always
Starlk
Starlk2y ago
But it is not really the problem here, I could easily fix it I'm more concerned about the memory usage
canton7
canton72y ago
You'll need to sort it yourself. From the docs:
The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.
Starlk
Starlk2y ago
I guess I can do a LINQ operation OrderBy
canton7
canton72y ago
I think it depends on whether you can stream an MagickImageCollection as you add frames to it. Since the Optimize call comes right at the end, I rather suspect you can't, but you'll have to take a close look at its API
Starlk
Starlk2y ago
Could you please elaborate more on this part:
I think it depends on whether you can stream an MagickImageCollection as you add frames to it.
I'm not sure if I understood it
canton7
canton72y ago
The problem is that MagickImageCollection is holding all of the frames in memory, until you call .WriteAsync, correct?
Starlk
Starlk2y ago
Yes, then I believe C# disposes it
canton7
canton72y ago
Right, but the thing is that MagickImageCollection wants to hold all of the frames in memory until it's ready to create the gif So the only way you can avoid that, is if MagickImageCollection has a way of not holding all frames in memory until it's ready to create the gif So you'll have to see whether it has an API which allows that
Starlk
Starlk2y ago
So it'd be like by adding the frames to the GIF file *dynamically* instead of having all of them ready in the memory
canton7
canton72y ago
Also, making Colors an array of SKPaint will reduce the amount of objects you need to create a lot, which will speed it up, but probably won't change the total memory usage Yeah, only if MagickImageCollection supports doing that. I don't know whether it does
Starlk
Starlk2y ago
That's a really nice idea, I will try to see if I can do it with either MagickImage or anything else. Thanks a lot!
canton7
canton72y ago
I don't know how animated gifs are structured internally. It might be impossible for all I know
i love cat(mull-rom splines)
when in doubt use the executable and start a subprocess
Starlk
Starlk2y ago
Hm? It does not seem that there is any library that allows me to do that, at least not in a straight-forward way https://legacy.imagemagick.org/discourse-server/viewtopic.php?p=60812&sid=76dbf1c9756e849d924f62c75308edc7#p60812 I guess I'm left with manipulating the GIF manually catsweat
Want results from more Discord servers?
Add your server
More Posts