Free Support Forum - aspose.com

Memory leak with Aspose.svg in C# .Net 5

I call the followed method in an api service and try to use Aspose.Svg library to create an Aspose.svg document:

 public async Task<PlanGroupCategory> UploadPlanAsync(byte[] byteStream, string mimeType)
 {
        if (byteStream == null)
            throw new Exception("Hinzugefügte Dateien sind leer.");

        using (MemoryStream stream = new MemoryStream(byteStream))
        {
            if (stream == null)
                throw new BauDrException("Svg Document ist leer.");
            stream.Seek(0, SeekOrigin.Begin);
            using (var document = new SVGDocument(stream, "."))
            {
                (...)
            }
        }
 }

After call using (var document = new SVGDocument(stream, “.”)) the application (ASP.Net 5) consums a lot of unmanaged memory. The memory jump from 200 MB to ± 1,3 GB and it will never be freed. After repeat the use case the memory goes up. The uploaded svg file from client is 2,6 MB. The memory leak was checked with visual studio diagnostics and JetBrains dotmemory. After calling the garbage collector the memory is not released. The call of individual “dispose” did not work either.

As described in stackoverflow (https://stackoverflow.com/questions/67065731/memory-leak-with-aspose-svg ), I post the memory leak right here in forum.
Used svg fie: 200911_OG1_Komplett.zip (172.9 KB)

@anis3h

We have tested the scenario in our environment while using Aspose.SVG for .NET 21.4 and noticed that the memory consumption was 200MB to 500MB and it went down after the SVG file was loaded. Could you please confirm if you are also using the latest version of the API. If so is the case and issue is still happening at your end, please share a sample console application with us which is able to replicate the same behavior. We will further proceed to assist you accordingly.

Thank you for your answer.
I tested the case with the last SVG.Net 21.4.
If I set a breakpoint before calling the following method:

public async Task UploadPlanAsync(byte[] byteStream, string mimeType, string fileName, Guid planGroupCategoryId)
{
if (byteStream == null)
throw new Exception(“Error.”);

        using (MemoryStream stream = new MemoryStream(byteStream))
        {
            if (stream == null)
                throw new Exception("Error.");
            //stream.Write(byteStream, 0, byteStream.Length);
            stream.Seek(0, SeekOrigin.Begin);
             //Breakpoint  !!!!!
                **using (var document = new SVGDocument(stream, "."))**
            {
                       (.....)
            }

}

After one step the memory looks like this:
Memory_Leak.png (32.2 KB)

I use the following technologies:
Angular 11 in frontend.
Asp.Net 5 (last version) in backend

The API Controller in ASP.Net 5 is look like this:

    [HttpPost, DisableRequestSizeLimit]
    [Route("upload/plancategories/{plangroupid}/plans")]
    public async Task<ActionResult<PlanGroupCategoryDto>> UploadPlan([FromRoute(Name = "plangroupid")] Guid planGroupId)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest("Error.");
        }

        var file = Request.Form.Files[0];

        byte[] byteStream;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (var stream = file.OpenReadStream())
            {
                await stream.CopyToAsync(memoryStream);
                byteStream = memoryStream.ToArray();
                var mimeType = file.ContentType;
                var fileName = file.FileName;

                var blob = new Blob
                {
                    FileName = fileName,
                    MimeType = mimeType,
                    BlobContent = new BlobContent { Content = byteStream }
                };

                var result = await _plansService.UploadPlanAsync(byteStream, mimeType, fileName, planGroupId);
                (...)
            }
        }
    }

The api controller is called from angular client with the svg file.
Can you share your sample console, please?