c# : simulate memory leaks

后端 未结 7 1033
囚心锁ツ
囚心锁ツ 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:25

    I created mine with this code:

    internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                var limit = 9400;
                while (true)
                {
                    var thread = new Thread(() => IncreaseMemory(limit));
                    thread.Start();
                }
            }
            catch (Exception)
            {
                // swallow exception
            }
    
        }
    
        private static void IncreaseMemory(long limity)
        {
            var limit = limity;
    
            var list = new List();
            try
            {
                while(true)
                {
                    list.Add(new byte[limit]); // Change the size here.
                    Thread.Sleep(1000); // Change the wait time here.
                }
            }
    
            catch (Exception ex)
            {
                // do nothing
            }
        }
    
    }
    

提交回复
热议问题