Skip to content

Instantly share code, notes, and snippets.

@lpenguin
Last active December 6, 2017 18:30
Show Gist options
  • Save lpenguin/adb0f55c17ffaddf608c70f27f5e268c to your computer and use it in GitHub Desktop.
Save lpenguin/adb0f55c17ffaddf608c70f27f5e268c to your computer and use it in GitHub Desktop.
MemEater
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace MemEater
{
static class MainClass
{
private const Int64 Kilobyte = 1024;
private const Int64 Megabyte = Kilobyte * 1024;
private const Int64 Gigabyte = Megabyte * 1024;
public static string StrSize(Int64 bytes)
{
if (bytes >= Gigabyte) {
return $"{bytes /(float) Gigabyte: 0.##} Gbytes ({bytes} bytes)";
}
if (bytes >= Megabyte) {
return $"{bytes / (float)Megabyte: 0.##} Mbytes ({bytes} bytes)";
}
if (bytes >= Kilobyte) {
return $"{bytes / (float)Kilobyte: 0.##} Kbytes ({bytes} bytes)";
}
return $"{bytes} bytes";
}
public static void Main (string[] args)
{
var start = DateTime.Now;
Int64 step = Megabyte;
if (args.Length == 1) {
step = int.Parse (args [0]);
}
Console.WriteLine ("Increment size: " + step + " bytes");
var items = new List<byte[]> ();
while(true) {
var data = new byte[step];
items.Add (data);
for (var j = 0; j < data.Length; j += 10) {
data [j] = 10;
}
if ((DateTime.Now - start).Seconds > 5)
{
start = DateTime.Now;
Console.WriteLine ("Items: " + items.Count);
// Console.WriteLine("Memory: " + StrSize(GC.GetTotalMemory (true)));
Process currentProc = Process.GetCurrentProcess();
Console.WriteLine ("Process size: " + StrSize(currentProc.PrivateMemorySize64));
Console.WriteLine ();
}
}
}
}
}
Usage:
mono MemEater.exe [step_in_bytes, default 1MB]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment