Note that there are some explanatory texts on larger screens.

plurals
  1. POGarbage collector test
    primarykey
    data
    text
    <p>I have written a simple .net forms application to test some behaviour of .NET about how it handles memory together with the garbage collector to do the cleaning.</p> <p>The forms application GUI looks like this:</p> <p><img src="https://i.stack.imgur.com/K1aTI.png" alt="Application GUI"></p> <p>And the code behind like this:</p> <pre><code>public partial class Form1 : Form { private readonly IList&lt;byte[]&gt; _btyList = new List&lt;byte[]&gt;(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int i = 0; while (i &lt; 3) { byte[] buffer = File.ReadAllBytes(@"C:\PFiles\20131018_nl_metro holland.pdf"); _btyList.Add(buffer); i++; } } private void button2_Click(object sender, EventArgs e) { int i = 0; while (i &lt; _btyList.Count) { _btyList[i] = null; i++; } } private void button3_Click(object sender, EventArgs e) { GC.Collect(); } } </code></pre> <p>When I add a couple of byte arrays to the private list of byte arrays it (of course) has effect on the memory usage of the application:</p> <p><img src="https://i.stack.imgur.com/ASY1P.png" alt="Memory usage after adding byte arrays"></p> <p>Now when I press the Clear memory button the memory usage will stay the same. I can wait for hours, but it doesn't change. If I press the Garbage collect button (after Clear memory) it will free the memory immediately:</p> <p><img src="https://i.stack.imgur.com/kFbiJ.png" alt="Memory usage after garbage collect"></p> <p>The question is: Why does the garbage collector not work in this case?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload