简体   繁体   中英

Performance of memory in winforms with C#

Before I open the SaveFileDialog Process Memory is running at 21MB and after opening the SaveFileDialog it jumps to 42MB and even after closing the DialogBox it does not return to the same value. 在此处输入图片说明

I don't think that this is a problem of new objects created because later in the process I create more instances and memory does not change at all. I am a bit worry about the memory usage as I have had this problem for long time now.

Why is this happening? or Is this "process memory" shown in the VS has anything to do with the memory consumption of the form? Here is my code inside the DialogBox

private void textBox4_Click(object sender, System.EventArgs e)
{
    using(SaveFileDialog saveFileDialog1 = new SaveFileDialog())
    {
        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.AddExtension = true;
        saveFileDialog1.RestoreDirectory = true;
        saveFileDialog1.DefaultExt = "txt";
        saveFileDialog1.CreatePrompt = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            saveFileDialog1.CheckFileExists = true;
            textBox4.Text = saveFileDialog1.FileName;
            filename = saveFileDialog1.FileName;
            try
            {
                using(StreamWriter s = new StreamWriter(filename))
                {
                    s.WriteLine(header);
                    s.Flush();
                }
            }
            catch (IOException)
            {
                MessageBox.Show("File is not accessible!");
            }
        }
    }
}

I had this program running for several hours and the value of 42MB never goes down.

This is normal and okay. It's not a new object sitting in memory. Instead, in order to use the SaveFileDialog , the program had to load a few additional assemblies from the system. So this new memory isn't part of the program's operation memory or working set... it's part of the program executional code itself.

The program won't unload these assemblies automatically unless the system is under actual memory pressure, where that memory (or memory address space) really is needed for something else. After all, you might need them again and taking the time to unload them would just slow the program down while it happens.

This is unrelated to the linked question. The issue in the link question is entirely about security boundaries, and has nothing to do with using too much memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM