0

I need to convert tif to webp and I am using wep wrapper class from this project - https://github.com/JosePineiro/WebP-wrapper/blob/master/WebPTest/WebPWrapper.cs

If I use regular for loop everything seems to be working correctly but I need to sped up the process and that is why I am using parallel for each to execute this method. I am getting out of memory exception in some cases. How can I avoid that? I was reading that I can use long memorySize = currentProcess.PrivateMemorySize64

Is this the best practice? I have never used this approach.

And I am looping it like:

Parallel.ForEach(MyList, (row) =>
{
    byte[] rawWebP;
    Image image = Image.FromFile(row.linkToImage);
    Bitmap bmp = (Bitmap)image;
    using (WebP webp = new WebP())
    rawWebP = webp.EncodeLossless(bmp, row.linkToImage);
});
  • Welcome to Stack Overflow. Please [edit] your question to include the full source code you have as a [mcve], which can be compiled and tested by others. Also include the full error message you get including the stacktrace. And explain what you are trying to do here. When you use the API, are you required to release the allocated memory by yourself or is it done by the API? Where is the documentation of the API you are using? – Progman Mar 30 '19 at 08:24
  • We also need to see the complete your side of the code, not the code of the library you are using. Please read [mcve] on how to provide a code sample, which can be compiled and tested by others. – Progman Mar 30 '19 at 08:37
  • I added my code as well – lio programista Mar 30 '19 at 09:06
  • Why is your `foreach` loop not using the `row` variable? And how many rows are we talking about? What size is the image you are trying to load? And how are you limiting/controlling the number of threads you are going to use for the `Parallel.ForEach()` loop? Maybe you need to set `MaxDegreeOfParallelism`, see https://stackoverflow.com/questions/36564596/how-to-limit-the-maximum-number-of-parallel-tasks-in-c-sharp. And it is still not a MCVE... – Progman Mar 30 '19 at 09:19
  • Thank you. It looks like limiting the threads that I am using in the parallel foreach to 4 fixed the issue. – lio programista Mar 30 '19 at 10:19
  • From my own experience to avoid memory problem you should use using as much as possible with disposable objects. I think your image is a disposable object – duc mai Mar 30 '19 at 10:29

0 Answers0