Using Aspose.Slides on Linux Container with .NET Core Does Not Release Memory

Thank you so much @andrey.potapov for sharing the docker updates.

Did your team also got a chance to review the sample ppt for which high memory issue still exists?

@ansamohdsh,
I forwarded your question to our developers and will get back to you as soon as possible.

@ansamohdsh,
As for the Sample.pptx file, the memory leak issue has also been fixed. It was tested with 500 iterations of loading and saving the presentation. The average memory consumption during testing ranged from 1 gigabyte to 1.35 gigabytes.

Hi @andrey.potapov,
Thank you for the updates.
I have a few more questions:

  • Is the memory leak issue fixed with the docker file changes you shared recently ?

  • Is it available for .net core 5 or .net core 7?

  • Do we need to upgrade to any specific Aspose Library version ?

@ansamohdsh,
The memory leak issue occurs under .NET 5.0 in the Linux environment. Under .NET 5.0, Aspose.Slides uses the System.Drawing.Common library, which in turn relies on the libgdiplus library. The libgdiplus library is part of Mono: libgdiplus | Mono. Investigations carried out for this task have revealed that the memory leak occurs in unmanaged memory in the libgdiplus library (version 6.0.4+dfsg-2). However, in version 6.0.5-0xamarin1+ubuntu2004b1, which is currently installed with Mono, the memory leak issue is not observed.

This problem can be reproduced outside of Aspose.Slides under .NET 5.0 by solely using System.Drawing.Common. Here is an example of code that reproduces the memory leak for .NET 5.0 in a Linux environment (the libgdiplus library is installed by default with the libgdiplus package):

for (int i = 0; i < 50; i++)
{
    using (Bitmap bmp = new Bitmap(8 * 1024, 8 * 1024))
    using (Graphics graphics = Graphics.FromImage(bmp))
    {
        graphics.Clear(Color.White);
    }
}; //-memory allocated for Bitmap is freed, but the memory for the Graphics object is not freed, graphics.Dispose() doesn't work. 

In the Dockerfile we provided for .NET 5.0, there have been added some commands to install the latest version of the libgdiplus library (6.0.5-0xamarin1+ubuntu2004b1) from the Mono repository:

# Install the latest version of libgdiplus library for aspnet:5.0, runtime:5.0 and aspnet:5.0-focal to resolve memory leak
    RUN apt-get update -q && \
    apt-get install -y -q gnupg ca-certificates && \
    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF && \

    #Uncomment this code for aspnet:5.0 and runtime:5.0. Comment it out for aspnet:5.0-focal because it doesn't work
    #echo "deb https://download.mono-project.com/repo/debian stable-buster main" | tee /etc/apt/sources.list.d/mono-official-stable.list && \
    #Uncomment this code for aspnet:5.0-focal. However, you should comment it out for aspnet:5.0 and runtime:5.0, as it doesn't work.
    echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | tee /etc/apt/sources.list.d/mono-official-stable.list && \

    apt-get update -q && \
    apt-get install -y -q libgdiplus

In .NET 6.0/7.0, the memory leak issue does not occur because of Aspose.Slides no longer uses System.Drawing.Common + libgdiplus in the Linux environment. Aspose.Slides uses its own implementation of System.Drawing. Microsoft has discontinued support for System.Drawing.Common on the Linux platform starting from .NET 6.0, and it is now only supported on the Windows platform. More details can be found here: Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn.

Yes, it’s fixed for .NET 5.0. There have been added some commands to the Dockerfile to install the latest version of libgdiplus to fix the memory leak. For .NET 6.0/7.0 this issue is not reproduced. For .NET 6.0/7.0 in the Dockerfile, there have been added commands to install necessary libraries used by Aspose.Slides, which may be missing in the Docker image:

#Install the necessary libraries for Aspose.Slides under .NET 6.0 and .NET 7.0, as they may be missing in the Docker image
RUN apt-get update && apt-get install -y \
 libfontconfig1 \
 libfreetype6 \
 libexpat1 \
 libpng16-16

The memory leak issue is available for .NET Core 5 in the Linux environment, but it is not available for .NET Core 7.

No, there is no need for that as the memory leak issue is not related to Aspose.Slides.

Note: there are some comments in the Dockerfiles for clarification.

Hello @andrey.potapov
we updated our docker files but fortunately we are still encountering the memory spikes.
Refer to the screenshots.23661ce6-e96e-47c0-94f1-aa79688346d3.gif (450.7 KB)
f0cfcd6d-cf3e-4db5-b514-b3f3bd36c3cc.gif (515.4 KB)

@ansamohdsh,
Please share your updated docker files. Then we will continue investigating the case.

.net 5.0 Docker File

FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base

#FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base

#FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base

# Uncomment to install the ttf-mscorefonts-installer for aspnet:5.0 and runtime:5.0. Please comment it out for aspnet:5.0-focal, as it doesn't 

#RUN echo "deb http://deb.debian.org/debian/ buster main contrib non-free" > /etc/apt/sources.list && \

    #echo "deb-src http://deb.debian.org/debian/ buster main contrib non-free" >> /etc/apt/sources.list

# Update package lists,  install the necessary packages and Update font caches

RUN apt-get update -q

ENV DEBIAN_FRONTEND=noninteractive

RUN echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections

RUN apt-get install -y -q ttf-mscorefonts-installer fontconfig curl

RUN fc-cache -vr

# Install the latest version of libgdiplus library for aspnet:5.0, runtime:5.0 and aspnet:5.0-focal to resolve memory leak

RUN apt-get update -q && \

    apt-get install -y -q gnupg ca-certificates && \

    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF && \

    #Uncomment this code for aspnet:5.0 and runtime:5.0. Comment it out for aspnet:5.0-focal because it doesn't work

    #echo "deb https://download.mono-project.com/repo/debian stable-buster main" | tee /etc/apt/sources.list.d/mono-official-stable.list && \

    #Uncomment this code for aspnet:5.0-focal. However, you should comment it out for aspnet:5.0 and runtime:5.0, as it doesn't work.

    echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | tee /etc/apt/sources.list.d/mono-official-stable.list && \

    apt-get update -q && \

    apt-get install -y -q libgdiplus

    



WORKDIR /app

COPY bin/Release/net5.0/publish/ /app/

RUN mkdir obj logs && chown nobody obj logs && apt-get remove -q -y curl

# RUN mkdir obj logs && chown nobody obj logs && apk del curl

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

# USER nobody

ENTRYPOINT ["dotnet", "Sample.dll"]

for .net 7 @andrey.potapov

#FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base

#FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base

#FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base

#FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base

#Uncomment to install the ttf-mscorefonts-installer for aspnet:6.0 (runtime:6.0) and aspnet:7.0 (runtime 7.0). Please comment it out for aspnet:x.x-focal, as it doesn't work.

 RUN echo "deb http://deb.debian.org/debian/ buster main contrib non-free" > /etc/apt/sources.list && \

    echo "deb-src http://deb.debian.org/debian/ buster main contrib non-free" >> /etc/apt/sources.list 

#The libgdiplus package can be removed if you'd like

RUN apt-get update -q && \

    DEBIAN_FRONTEND=noninteractive apt-get install -y -q ttf-mscorefonts-installer fontconfig libgdiplus curl && \

    fc-cache -vr

#Install the necessary libraries for Aspose.Slides under .NET 6.0 and .NET 7.0, as they may be missing in the Docker image

RUN apt-get update && apt-get install -y \

 libfontconfig1 \

 libfreetype6 \

 libexpat1 \

 libpng16-16



WORKDIR /app

COPY bin/Release/net7.0/publish/ /app/

RUN mkdir obj logs && chown nobody obj logs && apt-get remove -q -y curl

# RUN mkdir obj logs && chown nobody obj logs && apk del curl

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

# USER nobody

ENTRYPOINT ["dotnet", "Sample.dll"]

@ansamohdsh,
Thank you for the additional information. I’ve forwarded it to our developers. We will continue investigating the case.

Any updates @andrey.potapov

@ansamohdsh,
As far as I can see, our developers are working on the issue. Unfortunately, I don’t have any additional information yet.

@andrey.potapov
Do you have any updates for us now?

@ansamohdsh,
I’ve requested information from our developers for you. We will let you know soon. Thank you for your patience.

@ansamohdsh,
We’ve conducted a series of tests under .NET 7.0 using the presentations (DiagnosticReportExportTemplate.pptx and Sample.Pptx) you provided, and the previously supplied code sample. Unfortunately, the memory leak is not reproduced as a result of the testing. Could you please provide a sample project (code) and a presentation that reproduces the issue?

It was performed 1000 iterations of loading and saving the presentation both in single-threaded and multi-threaded modes. Memory consumption in the multi-threaded mode ranged from 1.7 to 4.5 GB (10 threads were simultaneously loading and saving presentations). During the testing process, memory usage increased very slowly – by +100 MB after every 100 iterations of loading and unloading presentations. However, this issue is also resolved by invoking the garbage collector via GC.Collect, and memory consumption stabilizes, staying in the range of 1.7-4.5 GB without any increases in the upper consumption limit.

Does memory consumption increase steadily over time, and does it eventually consume all available memory, leading to the application crashing due to memory exhaustion?

@andrey.potapov We are working on a POC where we will replicate the issue by showcasing how we create the components in our ppt.
Please allow us few more day and we will get back to you soon.

@ansamohdsh,
Thank you for the message. We will be waiting for your results and feedback.

GeneratePptSlides-31Aug-2023.zip (1.9 MB)
CustomTreeExport_20230831103949.zip (1.4 MB)

I have attached the sample poc and ppt generated from the poc.
We are able to replicate the issue on our side using this poc.
@andrey.potapov
Please use it as reference and call the endpoint multiple times (atleast 20-30 times) and try to replicate the issue at your end on linux.
Here in the poc we are using all the relevant pieces of aspose library classes which we use in our actual project code.

@ansamohdsh,
Thank you for the sample project and presentation files. I’ve forwarded it to our developers.

@andrey.potapov
hope you are doing well.
Please check with the developers and share the updates as soon as possible.