C
C#9mo ago
soziapath

C#, Process Memory slowly going up but snapshots not indicating memory leak

Hello, so I've been working on testing pythonnet for use in our companies application and now am doing a stresstest with a wpf application to find and patch any memory leaks. The program is basically just calling a python function that generates a sinewave and matplot figure of it, and returns the data series and the plot (double[] and byte[]), on the c# the plot is turned into a BitmapImage and set as ImageSource on a Image Control. The problem I am now encountering is that the process memory is slowly increasing, but using the diagnostics tool and looking at he heap size doesn't show any indication of a memory leak. Pretty new to C# and especially wpf, so maybe I'm simply missing something? The process starts out around 200mb Process Memory and after 15min it's at around 400. Image of the Snapshots and Process Memory attatched, as well as the relevant code snippets in a markdown file Would be great if someone has some insight into what might be wrong...
No description
15 Replies
soziapath
soziapathOP9mo ago
leowest
leowest9mo ago
if u change this line
var image = new BitmapImage();
var image = new BitmapImage();
to
using var image = new BitmapImage();
using var image = new BitmapImage();
does anything change? how do u know the leak is not coming from python? also your memory will always spike when u get a bigger image in size then the last one until the GC comes by and acts on it but since you're not disposing of your bitmapimage or using the usings it will remain in your code
soziapath
soziapathOP9mo ago
thanks for the reply. using on BitmapImage doesn't work, guess it's not IDisposable. Would be surprised if there were any more leaks in that short python code, anything I don't transfer to c# should be cleaned up by python gc.
leowest
leowest9mo ago
I indeed I misread it for a different class let me look at it again
soziapath
soziapathOP9mo ago
Okay, just provoked a memory leak in python and that doesn't show up on the snapshots. Guess I'm gonna look into the python code, though it is so short I don't really see any potential for any leaks...
leowest
leowest9mo ago
yeah I assume it would not show there but the c# side looks ok aside from the random fact that the memory will increase until the GC comes by to clear what needs to be clear
soziapath
soziapathOP9mo ago
Thanks for the help, guess it's gonna take some more time digging through everything to find what's causing the problem.
leowest
leowest9mo ago
sorry why do u need python
soziapath
soziapathOP9mo ago
For all the processing libraries. especially opencv, our main application is c#, but most of the computer vision engineers are better at using python for their processing needs.
leowest
leowest9mo ago
ok but if they give u the data u can use https://scottplot.net/cookbook/
ScottPlot Cookbooks
Cookbooks or organized by major version
leowest
leowest9mo ago
so u avoid a big chunk of issues instead of plotting on python to copy the image
soziapath
soziapathOP9mo ago
the plotting was just a easy stresstest of copying image data from python to c#. since we capture the images in c#, we have to move the data to python, there it is processed, segmented, annotated etc and the processed image is moved back to our c# side, so we really need the functionality of moving large data chunks. but that library looks nice, gonna remember that one
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
canton7
canton79mo ago
It's a stress test, that's probably fine? I know ScottPlot is the new hotness, but I still like Oxyplot. Easier to use and just as capable (if not more) IMO
soziapath
soziapathOP9mo ago
It's interesting, I call the gc in python and somehow this also triggers the c# gc
Want results from more Discord servers?
Add your server