I am currently very confused about my app :( . My app is taking up too much of memory..
Basically my app is a WPF application and it has 3-4 windows and a few pages,each one is opened/displayed when/as required.Each window/page has a couple of Image
controls to display images.To retrieve the images(from resource),i am using the following function :
public BitmapImage LoadImage(Uri uri, bool LoadOnMemory, int Pixel)
{
if (LoadOnMemory == true)
{
System.Windows.Resources.StreamResourceInfo sri = Application.GetResourceStream(uri);
System.IO.BinaryReader binReader = new System.IO.BinaryReader(sri.Stream);
byte[] buffer = binReader.ReadBytes(sri.Stream.Length);
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.Default;
bi.CreateOptions = BitmapCreateOptions.None;
bi.StreamSource = memoryStream;
bi.DecodePixelWidth = Pixel;
bi.EndInit();
bi.Freeze();
return bi;
}
}
else
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.Default;
bi.CreateOptions = BitmapCreateOptions.None;
bi.UriSource = uri;
bi.DecodePixelWidth = Pixel;
bi.EndInit();
bi.Freeze();
return bi;
}
}
I have,maybe like 20 images in total and to load each image,the function is called setting Pixel
to 30(or sometimes 40) and LoadOnMemory
to True
.My startup window has 2 images and one TextBlock
,same function is called to load both the images.
As soon as my app launches, in the Task Manager,i see that even the splash screen with only 2-3 lines of code to load the MainWindow
is eating up 105+ mb of RAM.When my MainWindow
loads,the consumption of RAM/Memory reaches upto 200+ mb.The main window calls the function couple o' times as there are almost 10 images in the MainWindow
.From there on,no matter how many Page
s or Window
s with any amount of image i open,the consumption of RAM stays the same.It,for some reason,doesn't cross 220+ mb.
I was suspecting that as my function uses MemoryStream
, this might be the reason for such a huge memory consumption.
But then i created a dummy window with no code-behind,just a blank window and launched my app......Even the blank window consumes 100+ mb of memory !!
But the dummy window is not my concern,my qs is , if for example, my app has like 20 images and each image is loaded using that function(or should i say the memorystream) , how much does it effect the ram ? (though i can see it's using almost 200+ mb).My function doesn't cause any lag/performance issue in the app but i just need to know if the function is responsible for such a huge memory consumption ?
UPDATE : Sorry for giving misleading info about the blank window
Till now i was running the app from within VS,so the total consumption of memory was actually the sum total of Intellitrace(eating up more than 30mb) and the Blank window(15-20 mb max....
One last qs, as i updated the code as well,which is better ? The 2nd part of the code(not using a memorystream
) or the first part(using the memoryStream
) in respect to both the app's performance and memory consumption ?
You certainly don't need an intermediate MemoryStream, when you could directly load the BitmapImage from the StreamResourceInfo's Stream:
private ImageSource LoadImage(Uri resourceUri, int pixelWidth)
{
var resource = Application.GetResourceStream(resourceUri);
var bitmap = new BitmapImage();
using (var stream = resource.Stream)
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.DecodePixelWidth = pixelWidth;
bitmap.EndInit();
bitmap.Freeze();
}
return bitmap;
}
However, you don't need to use a Stream at all, which is apparently simpler:
private ImageSource LoadImage(Uri resourceUri, int pixelWidth)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = resourceUri;
bitmap.DecodePixelWidth = pixelWidth;
bitmap.EndInit();
bitmap.Freeze();
return bitmap;
}