1

I am working on a code written by x-colleague. We have several Image files and we are converting them to XAML. The code is using XDocument to load in the image file (not of huge sizes but quite a lot of them) and do the processing on multi-thread. I have tried to look for every object which I think can be disposed once each iteration completes but still the issue is there, If I keep running the process it consumes the RAM fully and then Visual Studio crashes, what surprises me most is once this happened then I am unable to open anything on my PC, every single thing complains about memory is full including Visual Studio.

I am unable to upload the image here.

What I have tried it to run it on a single thread, though I encounter GC pressure but I am still able to run the code and memory stays good until the end.

I know I need to look for alternative instead of using XDocument but that is out of scope at the moment and I need to work through the code.

Can You please help me or give me some pointers?

Code below is how I load the XML before sending it to API for processing:

XDocument doc;
using (var fileStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(Image1.sv.ToString())))
{   
    doc =  XDocument.Load(fileStream);
}

API then uses multi-threading to process the image file to convert to XAML using different methods, each of these are using XDocument, its loading via memory stream, save in memory and continued the processing.

I have used Diagnostic Tools within VS to identify the memory leak.

Kind regards

Muhammad Sulaiman
  • 2,399
  • 4
  • 14
  • 28
DataNuker
  • 11
  • 1
  • 1
    using lots of memory != memory leak; it is impossible to discuss whether a memory leak is happening without a *lot* more context and detail, but we should assume "probably not", at least: not in the code shown here – Marc Gravell Oct 13 '22 at 13:00
  • I have clearly noticed it is happening, If You follow this link as I am unable to upload any images, it clearly shows the Memory Leak and also GC Pressure, I managed to avoid Memory leak but as a result I have entered into GC Pressure scenario https://michaelscodingspot.com/find-fix-and-avoid-memory-leaks-in-c-net-8-best-practices/#:~:text=Detect%20Memory%20Leak%20problems%20with,of%20memory%20your%20process%20uses. – DataNuker Oct 13 '22 at 13:18
  • It's likely the memory leak is happening elsewhere in the code, such as the point where you actually generate the image on the screen. – Charlieface Oct 13 '22 at 15:42
  • You do not have a memory leak. SVG files are huge and will give memory error unless you use XmlReader. I done this lots of times. Best way is to use a combination of XmlReader and Xml Linq. See my example here : https://stackoverflow.com/questions/61607180/parse-big-xml-file-using-xmlreader – jdweng Oct 14 '22 at 00:54
  • Both Marc and Charlie have seen this issue before and have seen my code. Even have posted it as a solution. It is not a memory leak. XDocument is not designed to handle huge xml. – jdweng Oct 14 '22 at 00:59

1 Answers1

0

The new MemoryStream(Encoding.ASCII.GetBytes(someString)) step seems very redundant, so we can shave a lot of things by just... not doing that, and using XDocument.Parse(someString):

var doc = XDocument.Parse(Image1.sv.ToString());

This also avoids losing data by going via ASCII, which is almost always the wrong choice.

More savings may be possible, if we knew what Image1.sv was here - i.e. it may be possible to avoid allocating a single large string in the first place.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thank You very much for Your swift response, really appreciated. Images are basically one SVG file for which XDocument is used to read it as an XML and then further process to split the file and convert it to XAML. Each SVG image/file contains several other small SVGs images/files which are extracted using the XDocument into individual files. for example 1 SVG may contain 10 SVGs with a unique ID which through this API using a single SVG we are splitting these into individual SVGs. – DataNuker Oct 13 '22 at 13:07
  • @DataNuker ok; so `Image1.sv` is some kind of object model over an SVG; great; **what exact** type are you using here? Again, there may be some way to bypass the string – Marc Gravell Oct 13 '22 at 13:10
  • Thank You once again, SVG is an XML-based vector image format for two-dimensional graphics. Code is reading the tag as an XML element. So for the original image which may contains around 10 another images each are tagged using "SVG" which have further nodes, the root is this SVG. – DataNuker Oct 13 '22 at 13:29
  • @DataNuker I don't think you understood. C# is a language of types, SVG and XML are not C# types. What C# type is `Image1.sv`? What is `Image1`? – NetMage Oct 13 '22 at 14:22
  • My sincere apologies, I understand now, Its an XDocument type, here is the property: public XDocument Image1 { get; set; } – DataNuker Oct 13 '22 at 15:05
  • 1
    @DataNuker if it is an `XDocument` *why are you doing anything*? you *already have the doc*; there is nothing left to do; `var doc = Image1; // job done` – Marc Gravell Oct 13 '22 at 15:08
  • @DataNuker Agreed, this whole parsing thing seems unnecessary. – Charlieface Oct 13 '22 at 15:42