c# : simulate memory leaks

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

    The below addresses what you want in (a) a Gradual Memory Leak from a Console App where you can set the amount to leak and the duration to leak it.

    1. Console App That Gradually Eats Up Memory
    2. Parameter #1 is the Memory it will Eat Up in Megabytes (ie 6000 is 6 Gigs)
    3. Parameter #2 is the Gradual Delay for each Iteration (ie 1000 is 1 second)
    4. The Committed Memory and Working Set will be around the Same

    It was designed to use XmlNode as the object that takes up Memory because then the COMMITTED MEMORY (memory allocated by process in OS) and WORKING SET MEMORY (memory actually used by the process) would be the same. If a primative type is used to take up memory such as a byte[] array, then the WORKING SET usually is nothing, because the memory is not actually be used by the process even though its been allocated.

    Make sure to compile in x64 under the Project Properties under the Build Tab. Otherwise if its compiled in x32 then it will get an OutOfMemory error around the 1.7Gigs limit. In x64 the Memory it eats up will be pretty "limitless".

    using System;
    using System.Xml;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;
    
    namespace MemoryHog
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("    Total Memory To Eat Up in Megs: " + args[0]);
                Console.WriteLine("Millisecs Pause Between Increments: " + args[1]);            
                int memoryTotalInMegaBytes     = Convert.ToInt32(args[0]);
                int secondsToPause             = Convert.ToInt32(args[1]);
    
                Console.WriteLine("Memory Usage:" +     Convert.ToString(GC.GetTotalMemory(false)));
    
                long runningTotal = GC.GetTotalMemory(false);
                long endingMemoryLimit = Convert.ToInt64(memoryTotalInMegaBytes) * 1000 *     1000;
                List memList = new List();
                while (runningTotal <= endingMemoryLimit)
                {
                    XmlDocument doc = new XmlDocument();
                    for (int i=0; i < 1000000; i++)
                    {
                        XmlNode x = doc.CreateNode(XmlNodeType.Element, "hello", "");
                        memList.Add(x);
                    }
                    runningTotal = GC.GetTotalMemory(false);
                    Console.WriteLine("Memory Usage:" +     Convert.ToString(GC.GetTotalMemory(false)));
                    Thread.Sleep(secondsToPause);
                }
                Console.ReadLine();
                
            }
    
        }
    }
    

    And as Brian Gideon's Answer already recommended, you can call this by using the following code and then killing the process your part (b). The example below calls MemoryHog.exe (the program from the code above) to eat up 6 Gigs and pausing every 2 seconds

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading.Tasks;
    
    namespace ProcessLauncher
    {
        class Program
        {
            static void Main(string[] args)
            {
                Process process = Process.Start("MemoryHog.exe", "6000 2000");
                Console.Read();
                process.Kill();
    
            }
        }
    }
    

提交回复
热议问题