Reduce Memory allocation of an import window #16251
-
This is a stack trace of an import window I have been working on, and most of the allocation you are seeing is just a stack trace of me executing a function that searches a SQL database for specific data and displays that data. That data contains a bunch of images and small amounts of data that get displayed as text. my issue is that once the images and text have been loaded into memory to display, it's allocated more the 100mb of memory. It doesn't matter how small the image is, and where the image gets pulled from, like from the web or from storage itself, it's always allocating that much memory. And that amount of allocation seems excessive when I think about having to do that for thousands of images and text down the line. And it comes even worse than that, when I close the import window, and return to the main window of the program, the allocation stays, and garbage collection isn't removing it either, which is a significant memory leak. So I am here to ask what could be causing that amount of allocation and how I can reduce it and purge it once I am done with it? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Be mindful that when your images are displayed on screen (ie. backed by You don't purge memory in .NET. That's the job of the GC. 50-100MB is a drop in the bucket on a modern system. No memory pressure on the GC means it's unlikely to wake up and collect. Now if you keep entering/exiting the import, get several GBs of memory footprint, and it's not collecting...that's more indicative of an actual leak. Memory profiling tools in Rider / VS can assist in debugging these sort of issues. |
Beta Was this translation helpful? Give feedback.
There is some further discussion here about memory usage and bitmaps.
#14304
But in general as long as you are using a virtualised list, are not showing a huge amount of them at once, all the bit maps are the same size, and you lazy load everything, memory usage should stay fairly solid when scrolling through the list and be somewhat reasonable. I was able to get memory usage down even further in my case by force disposing bitmaps in my codebehind when they were removed from the virtualised list. Using
Bitmap.Decode
also seems to make memory usage far worse than just resizing them ahead of time and then caching the result to disk.