c# : simulate memory leaks

后端 未结 7 1030
囚心锁ツ
囚心锁ツ 2021-02-05 17:26

I would like to write the following code in c#. a) small console application that simulates memory leak. b) small console application that would invoke the above application and

7条回答
  •  Happy的楠姐
    2021-02-05 18:26

    I have tried to use the approach, described in the accepted answer, but it didn't work - it looks like compiler or runtime have optimized this away.

    I have found the simplest modification which makes this work:

    var rnd = new Random();
    var list = new List();
    while (true)
    {
        byte[] b = new byte[1024];
        b[rnd.Next(0, b.Length)] = byte.MaxValue;
        list.Add(b);
    
        Thread.Sleep(10);
    }
    

    This code makes application consume more and more memory.

提交回复
热议问题