c# : simulate memory leaks

后端 未结 7 866
囚心锁ツ
囚心锁ツ 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条回答
  •  执笔经年
    2021-02-05 18:17

    The leaking application might look like:

    public static void Main()
    {
      var list = new List();
      while (true)
      {
        list.Add(new byte[1024]); // Change the size here.
        Thread.Sleep(100); // Change the wait time here.
      }
    }
    

    And the calling application might look like:

    public static void Main()
    {
      Process process = Process.Start("leaker.exe");
      process.Kill();
    }
    

    Take a look at the properties on the Process class. There are several that return how much memory the process is consuming. You may be able to use one of them to conditionally kill the process.

提交回复
热议问题