Have a good day.
I’m working with scichartsurface, where I’m drawing a lot of lines with the help of IRenderContext2D. I’ve found out that this way of drawing took too much memory. For example when I displayed only surface, it took only ~80 Mb (Image 1), but when I displayed the graph using checkbox, began to scroll and pan my graph, the usage of memory increased to ~160 Mb (Image 2).
What is the reason of such behaviour?
Thanks in advance
I’ve attached the project: https://yadi.sk/d/cYP6RFVXhfjRS
- Egor asked 8 years ago
- You must login to post comments
Hi Egor,
I investigated your sample attached, and while I do see an increase in memory usage when panning, this memory is quickly reclaimed by the garbage collector, so I do not believe there is a memory leak.
A memory leak is a “gradual loss of available computer memory when a program (an application or part of the operating system) repeatedly fails to return memory that it has obtained for temporary use.”
As you can see below, SciChart is returning the memory as soon as the garbage collector runs (which is decided by .NET, not by us).
Below: Memory is reclaimed when the Garbage Collector runs
I have modified your sample to perform a garbage collection to test if memory is released. You can verify this by adding this code to your application:
XAML
<StackPanel Orientation="Horizontal">
<CheckBox Margin="5" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" IsChecked="{Binding IsCheckedDisplay}" Content="Display Graph"/>
<Button Margin="5" Content="Garbage Collect?" Click="ButtonBase_OnClick"></Button>
</StackPanel>
CSharp
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
Let me know if you see the same.
What causes memory growth in your application?
As for what causes this memory usage to grow so quickly, if we comment out this code in yoru application, then the memory stays stable
private void OnRedrawWellSection(object sender, IRenderContext2D e)
{
// ...
}
So I suggest investigating this area for memory problems. One line of code which looks suspect is this:
WriteableBitmap writeableBmp = BitmapFactory.New(width, height);
Creating a new bitmap every single redraw will cause unmanaged memory to grow rapidly. I would suggest caching this bitmap and only recreating if the size changes.
Best regards,
Andrew
- Andrew Burnett-Thompson answered 8 years ago
- last edited 8 years ago
- Thank you for the fast answer. I've used your advices, and it has saved 20 mb of memory, but there is one more question: Is it normal, that the memory usage increases from ~50 Mb to ~600 Mb, while the surface is being panned?
- Hi Egor, no it is not normal at all! If you see our demos this doesn't occur in any of our examples. The memory usage is occurring in your code (OnRedrawWellSection). I am not sure exactly where or why. Something else you should know. In .NET the garbage collector only runs when memory is needed. You should find if you let your application run that it never runs out of memory, the .NET GC should run before this occurs. Let me know if this helps.
- Sorry for the long response. On some computers there is a error. Here is a log: ERROR Thread-'1' Method OnRedrawWellSection had an error @ 10.07.2015 12:25:56: Insufficient memory to continue the execution of the program в System.Windows.Media.Imaging.RenderTargetBitmap.FinalizeCreation() в System.Windows.Media.Imaging.RenderTargetBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight, Double dpiX, Double dpiY, PixelFormat pixelFormat) в A..(FrameworkElement ) в Abt.Controls.SciChart.Rendering.TextureCache.GetWriteableBitmapTexture(FrameworkElement fe) в A..CreateSprite(FrameworkElement fe) в Geonaft.Modules.CS.ViewModels.WellSectionViewModel.OnRedrawWellSection(Object sender, IRenderContext2D e) Is there any limitation to create sprites?
- No there is no limitation on creating sprites, so long as they are disposed. I noticed in your code you are calling dispose on a sprite so this shouldn't be a problem. As I said, I don't think there is a memory leak here, but instead, memory is being used very quickly and the GC is unable to keep up. If you look at our examples you will notice none of them do this, so I reckon the problem may lie in your code OnRedrawWellSection(). If you can prove me wrong, please do, we will be happy to fix any issues in SciChart!
- You must login to post comments
Hello again!
I’ve modified my project to run faster. I’ve put BackgroundImageAnnotation, from which we create sprite, outside the function. But when I run the application, the image freezes and doesn’t redraw.
What is the reason? And how can I fix it?
I’ve attached modified project below.
- Egor answered 8 years ago
- last edited 8 years ago
- You must login to post comments
Please login first to submit.