Skip to content

Memory held by the resources is not released even after disposing all the references in WPF when disabling prefer-32 bit in the application #1082

@vsfeedback

Description

@vsfeedback

Hi,

I created a WPF application with byte array, memory stream and BitmapImage when a main page is loaded as in the below snippet.

public partial class MainWindow:Window     
{         
byte[] buffer;         
Stream stream;         
BitmapImage bitmap;         

public MainWindow()
{
InitializeComponent();
this. Loaded+=MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender,RoutedEventArgs e)
{
buffer =newbyte[100000000];
stream =newMemoryStream(buffer);
bitmap =newBitmapImage();
bitmap. BeginInit();
bitmap. UriSource=newUri(@"Image file Path");
bitmap. EndInit();
}
}

When the main page is loaded, I noted down the memory (using Visual Studio Diagnostic tool) is raised to 160 MB.

I wrote a button click event and cleared all the resources properly in the event handler as in the below code snippet.

private void button_Click(object sender,RoutedEventArgs e)
{
Array.Clear(buffer,0, buffer. Length);
buffer =null;
stream. Close();
stream. Dispose();
bitmap. UriSource=null;
bitmap =null;
}

When test the memory in Visual Studio Diagnostic tool, I could see that the memory held by the resources is not released even after cleared and the memory stays at 160 MB.

When tried to call the Garbage collection as in the below code snippet, I could see only 5 MB is reduced and the memory still stays at 155 MB.

GC. Collect();
GC. WaitForPendingFinalizers();

I suspect memory leak in this. Can you please suggest me how to release the total memory held by the resources?

Note: Disable the Prefer-32 bit in the Application

MSDN forum reference link (for more details): https://social.msdn.microsoft.com/Forums/vstudio/en-US/4b519b48-e9c8-4a66-b051-ce8a0acde2a7/memory-held-by-the-resources-is-not-released-even-after-disposing-all-the-references-in-wpf?forum=wpf

Regards,

Deepak G

This issue has been moved from https://developercommunity.visualstudio.com/content/problem/620424/memory-held-by-the-resources-is-not-released-even.html
VSTS ticketId: 937073

These are the original issue comments:
(no comments)
These are the original issue solutions:
(no solutions)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions