C #: simulate memory leaks

I would like to write the following code in C #. a) a small console application that simulates a memory leak. b) a small console application that will reference the above application and immediately release it, simulating a memory leak problem.

In other words, application (b) constantly called and freed application (a) to mimic how the β€œrebel” application of a memory leak application is contained without addressing the root cause, which is application (a).

Some sample code for applications (a) and (b) will be very useful.

thanks

+6
c # memory memory-leaks
source share
6 answers

A leaking application might look like this:

public static void Main() { var list = new List<byte[]>(); 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 this:

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

Look at the properties in the Process class . There are several that return the amount of memory consumed by a process. You can use one of them to conditionally kill a process.

+11
source share

Just create IDisposable objects and don't destroy them! Examples are Image Graphics or any kind of handle or code that comes out of unmanaged code to create / manage resources.

+3
source share

You can simulate a memory leak by simply adding objects to the global list by timer.

+1
source share

Listed below are what you want in (a) a gradual memory leak from the console application, where you can set the amount of leak and its duration.

  • A console application that gradually flushes memory.
  • Parameter No. 1 is the memory that it will consume in megabytes (i.e. 6,000 - 6 gigs).
  • Parameter No. 2 is a gradual delay for each iteration (i.e. 1000 - 1 second)
  • Designated memory and working set will be around the same

It was designed to use XmlNode as an object that takes up memory, because then COMMITTED MEMORY (memory allocated by the process on the OS) and WORKING SET MEMORY (memory actually used by the process) will be the same. If the primitive type is used to capture memory, such as the byte [] array, then WORKING SET is usually nothing, because the memory is not actually used by the process, even if it was allocated.

Be sure to compile to x64 in the Project Properties section of the Build tab. Otherwise, if it is compiled in x32, then it will get an OutOfMemory error around the 1.7Gigs limit. In x64, the memory he eats will be quite "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<XmlNode> memList = new List<XmlNode>(); 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 has already recommended, you can call about this using the following code and then kill the process of your part (b). The example below calls MemoryHog.exe (the program from the above code) to eat 6 Gigs and pause 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(); } } } 
0
source share

I created my 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<byte[]>(); 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 } } } 
0
source share

I tried using the approach described in the accepted answer, but it did not work - it seems that the compiler or runtime optimized this.

I found the simplest modification that does this job:

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

This code makes the application consume more and more memory.

0
source share

All Articles