You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue Description Description
Our application is experiencing high memory consumption, leading to Out of Memory (OOM) exceptions. After extensive debugging and analysis with the Microsoft support team, we have identified that the SkiaSharp library is the primary driver of the excessive memory usage.
Steps to Reproduce
Deploy the application using SkiaSharp 2.88.8 in a Docker container with the official .NET SDK 8.0 image.
Run the application under typical load, processing images.
Monitor memory usage over time.
Expected Behavior
Memory usage should remain stable and within acceptable limits, without leading to OOM crashes.
Actual Behavior
Memory usage continually increases over time, ultimately causing the application to crash with an OOM exception. The issue is most prevalent when processing images, which suggests that SkiaSharp's memory management might be the root cause.
Evidence and Analysis
Heap Dumps: Multiple heap dumps indicate significant memory consumption in PAGE_READWRITE regions, which is likely attributed to native memory allocations.
Heaptrack Analysis:
Peak memory consumers show SkiaSharp's native library (libSkiaSharp.so) consuming large amounts of memory.
At its peak, SkiaSharp allocated ~250MB of RAM over 1257 calls, with specific functions like sk_bitmap_try_alloc_pixels being notable contributors.
Environment
SkiaSharp Version: 2.88.8
.NET Version: 8.0.5
OS: Linux Debian (Docker Container)
Image: mcr.microsoft.com/dotnet/sdk:8.0
Relevant Files
ImageHelper.cs: The primary usage of SkiaSharp within our application.
Heap Dumps and Heaptrack Traces: Available upon request.
Any advice on configuration changes or code adjustments to mitigate the issue would be appreciated.
Updates: Information on any known issues or updates in SkiaSharp that address this problem.
Code
publicclassImageHelper:IImageHelper{publicbyte[]?ResizeImage(byte[]img,SizemaxSize){try{// If the size is 0, return the default image sizeif(maxSize.IsEmpty)returnimg;usingMemoryStreamms=new MemoryStream(img);usingSKCodecskCodec= SKCodec.Create(ms);usingSKBitmapsourceBitmap= SKBitmap.Decode(skCodec);// Rotate if neededusingSKBitmaprotatedBitmap= HandleOrientation(sourceBitmap, skCodec.EncodedOrigin);// Calculate the new sizevarnewSize= CalculateNewSize(maxSize,new Size(rotatedBitmap.Width, rotatedBitmap.Height));SKFilterQualityquality= SKFilterQuality.Medium;usingSKBitmapscaledBitmap= rotatedBitmap.Resize(new SKImageInfo(newSize.Width, newSize.Height), quality);usingSKImagescaledImage= SKImage.FromBitmap(scaledBitmap);usingSKDatadata= scaledImage.Encode(SKEncodedImageFormat.Jpeg,75);return data?.ToArray();}catch(Exceptionex){
Console.WriteLine($"Error while resising the picture: {ex}");returnnull;}}privatestatic SKBitmap HandleOrientation(SKBitmapbitmap,SKEncodedOriginorientation){SKBitmaprotated;switch(orientation){case SKEncodedOrigin.BottomRight:using(varsurface=new SKCanvas(bitmap)){
surface.RotateDegrees(180,(float)bitmap.Width /2,(float)bitmap.Height /2);
surface.DrawBitmap(bitmap.Copy(),0,0);}returnbitmap;case SKEncodedOrigin.RightTop:rotated=new SKBitmap(bitmap.Height, bitmap.Width);using(varsurface=new SKCanvas(rotated)){
surface.Translate(rotated.Width,0);
surface.RotateDegrees(90);
surface.DrawBitmap(bitmap,0,0);}returnrotated;case SKEncodedOrigin.LeftBottom:rotated=new SKBitmap(bitmap.Height, bitmap.Width);using(varsurface=new SKCanvas(rotated)){
surface.Translate(0, rotated.Height);
surface.RotateDegrees(270);
surface.DrawBitmap(bitmap,0,0);}returnrotated;default:returnbitmap;}}privatestatic Size CalculateNewSize(SizemaxSize,SizecurrentSize){varnewSize= currentSize;// Check the heightif(newSize.Height > maxSize.Height){
newSize.Height = maxSize.Height;
newSize.Width = currentSize.Width * newSize.Height / currentSize.Height;}currentSize=newSize;// Check the widthif(newSize.Width > maxSize.Width){
newSize.Width = maxSize.Width;
newSize.Height = newSize.Height * newSize.Width / currentSize.Width;}returnnewSize;}}
Code of Conduct
I agree to follow this project's Code of Conduct
The text was updated successfully, but these errors were encountered:
Description
Issue Description
Description
Our application is experiencing high memory consumption, leading to Out of Memory (OOM) exceptions. After extensive debugging and analysis with the Microsoft support team, we have identified that the SkiaSharp library is the primary driver of the excessive memory usage.
Steps to Reproduce
Expected Behavior
Memory usage should remain stable and within acceptable limits, without leading to OOM crashes.
Actual Behavior
Memory usage continually increases over time, ultimately causing the application to crash with an OOM exception. The issue is most prevalent when processing images, which suggests that SkiaSharp's memory management might be the root cause.
Evidence and Analysis
Environment
Relevant Files
Any advice on configuration changes or code adjustments to mitigate the issue would be appreciated.
Updates: Information on any known issues or updates in SkiaSharp that address this problem.
Code
Code of Conduct
The text was updated successfully, but these errors were encountered: