0

In one of my SSIS package giving error during the execution time. Package contains two script task which will run in parallel. From script task what I'm doing is that, getting some svg files from a shared location and read it and then convert it to xaml and png files. Inside script task there are two loops. One loop is to iterate the svg files from the folder. Second loop is for each svg files will be multiple root elements which are related to some assembly information. So iterating through these root elements will create individual xaml files for each one. In one svg file at least 5 or more components will be there.

This is working fine for limited files around 20 or 25. But when increase the count to 100 or 150 that time I'm getting error like "Not enough memory resources to perform this command". When I look in the memory and resource governor there is no any memory hits.

Could some one help what else I'm need to look to figure out the issue.

enter image description here

    public void UploadPng(string svgPath, string svgFileName)
    {
        //var outputFile = XDocument.Load(svgPath);
        string pngPath = Dts.Variables["PNGFolder"].Value.ToString() + @"\snapshot_" + svgFileName.Replace("svg","png");            
        var htmlToImageConv = new NReco.ImageGenerator.HtmlToImageConverter
        {
            Width = 600,
            Height = 400
        };
   
        var jpegBytes = htmlToImageConv.GenerateImageFromFile(svgPath, ImageFormat.Png); //htmlToImageConv.GenerateImage(html, ImageFormat.Png);            
        using (var stream = new MemoryStream(jpegBytes, 0, jpegBytes.Length))
        {
            Bitmap bm = new Bitmap(Image.FromStream(stream));
            bm.Save(pngPath, System.Drawing.Imaging.ImageFormat.Png);
            stream.Dispose();
            stream.Close();
        }
    }

// For xaml writing

  public  void UploadXAMLFile(SVgComponent Component)
    {         
      
        using (var fileStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(cnt + Component.xaml.ToString())))
        {
            filesize = Convert.ToString(fileStream.Length);
            var FileTransfer = new TransferUtility(new AmazonS3Client(_accessKey, _secretKey, _bucketRegion));
            FileTransfer.Upload(fileStream, path + Component.svgName, Component.FileName.Replace("svg", "xaml").ToLower());
            fileStream.Dispose();
            fileStream.Close();
        }            

    }
Sreepathi
  • 93
  • 13
  • Without any code to look at we could only guess. But if I were to guess... `System.Drawing.Image` and all its descendant classes are `IDisposable`, meaning that they should be used in `using (...) { ... }` blocks so that their resources are disposed of as soon as they go out of scope. Are you using using blocks? – AlwaysLearning Aug 26 '22 at 10:04
  • For png conversion I'm using a third party dll. NReco.ImageGenerator. using (var stream = new MemoryStream(jpegBytes, 0, jpegBytes.Length)) { Bitmap bm = new Bitmap(Image.FromStream(stream)); bm.Save(pngPath, System.Drawing.Imaging.ImageFormat.Png); stream.Dispose(); stream.Close(); } – Sreepathi Aug 26 '22 at 10:20
  • Please [Edit](https://stackoverflow.com/posts/73499209/edit) your question to include code examples. The comments section is not suitable for formatted code blocks. – AlwaysLearning Aug 26 '22 at 10:25
  • Posted the code, but it is not the complete code. – Sreepathi Aug 26 '22 at 10:32

1 Answers1

0

As mentioned in the comments, System.Drawing.Image and all of its descendant classes are IDisposable. To handle IDisposable classes correctly the following code:

using (var stream = new MemoryStream(jpegBytes, 0, jpegBytes.Length))
{
    Bitmap bm = new Bitmap(Image.FromStream(stream));
    bm.Save(pngPath, System.Drawing.Imaging.ImageFormat.Png);
    stream.Dispose();
    stream.Close();
}

Should be changed to:

using (var stream = new MemoryStream(jpegBytes, 0, jpegBytes.Length))
using (var img = Image.FromStream(stream))
using (var bm = new Bitmap(img))
{
    bm.Save(pngPath, System.Drawing.Imaging.ImageFormat.Png);
}

Check the documentation for other classes you use to see if they are also IDisposable and need to be handled similarly.

AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35
  • Thanks for this. I gone through other classes but they are not in IDisposable format. – Sreepathi Aug 26 '22 at 12:43
  • Looping for long time will cause these memory issue ? In my case there are two loops. In the first loop it will iterate 200 items and in the inner loop that minimum 5 and maximum 20 times. – Sreepathi Aug 26 '22 at 12:46