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
Directory.GetFiles("Backups")
-- that doesn't return the results in any guaranteed orderIt apparently starts from the top to bottom
The order is not guaranteed. The order probably comes from the filesystem itself, which IIRC is usually file creation order, but not always
But it is not really the problem here, I could easily fix it I'm more concerned about the memory usage
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.
I guess I can do a LINQ operation OrderBy
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 APICould 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
The problem is that
MagickImageCollection
is holding all of the frames in memory, until you call .WriteAsync
, correct?Yes, then I believe C# disposes it
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 thatSo it'd be like by adding the frames to the GIF file *dynamically* instead of having all of them ready in the memory
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 doesThat's a really nice idea, I will try to see if I can do it with either MagickImage or anything else. Thanks a lot!
I don't know how animated gifs are structured internally. It might be impossible for all I know
when in doubt use the executable and start a subprocess
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