Hello and Thanks for any answers!
I am not a .NET person although, I can code OK or understand an existing C# code, since I am a C/C++ person. Sorry for the bit long text, but didn't have a choice.
I wrote a simple .NET application as follows. That's the full code below. The form is very small with just one button. See the simple btn code below. First time I click on the btn, the Windows File selection dialog shows, I hit cancel on that dialog. I see memory go up by 6 MB (from 15MB to 21 MB). I did this for about 10 times. From the second an subsequent times, the memory kept increasing by small amounts (few hundred Ks) and total went up to 23 MB or so. The memory used never decreased. I left the app for an hour thinking memory will come back. It didn't. With the GC collector code below, the first time memory increase is same, but subsequently, it it didn't increase memory, it would go and down but not below 21 MB.
Even when I removed all the code inside the button click and kept clicking the button memory would slowly go up by 0.1 MB in the Windows Task Manager, every few clicks. Never comes back. But when I just put the GC collect code alone in the Btn click code (nothing else), the memory doesn't increase in the task manager.
My question is, why is the 6 MB is permanently lost even though the Windows file dialog is closed? Windows Task manager for this app shows the same memory increase effect (although the memory shown is different size for valid reasons compared to the one shown thru the app). OR will .NET release memory some other day, or it will only do something if it realizes application process memory is in dangerously high.
Reason to ask this question is, we have a .NET UI App running in a machine that is used by several users to kick of jobs 2 shifts a day for months without exiting the app since we cannot exit the app (for reasons). The App will create a .NET worker class/function does the work/job, exit the worker, and be ready for next job. Every time users kick of something, the memory keeps on increasing causing issues after a month or so depending the frequency of usage. I am not .NET person. I thought its not that easy leak memory in .NET :-) That's why wrote a simple .NET/C# test. I see that memory is getting lost easily :-)
Any help with sample test app? or its just how .NET works and there is nothing apart from adding the GC collector code everywhere in the app to mitigate the memory usage increase?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process currentProcbefore = Process.GetCurrentProcess();
long memoryUsedbefore = currentProcbefore.PrivateMemorySize64;
var filePath = string.Empty;
//FolderBrowserDialog test = new FolderBrowserDialog();
//if (test.ShowDialog() == DialogResult.OK)
//{
// //Get the path of specified file
// filePath = test.SelectedPath;
//}
using (FolderBrowserDialog test = new FolderBrowserDialog())
{
if (test.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = test.SelectedPath;
}
}
//GC.Collect();
//GC.WaitForPendingFinalizers();
//GC.Collect();
Process currentProcafter = Process.GetCurrentProcess();
long memoryUsedafter = currentProcafter.PrivateMemorySize64;
MessageBox.Show("Mem Usage before:" + memoryUsedbefore + " After:" + memoryUsedafter);
}
}
Thanks Jacob