Last active
December 8, 2017 11:26
-
-
Save lpenguin/79e13ef0df062a6eae74b2e9d239a930 to your computer and use it in GitHub Desktop.
MemEater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading; | |
namespace MemEater | |
{ | |
static class MainClass | |
{ | |
private static readonly Random Random = new Random(); | |
private const Int64 Kilobyte = 1024; | |
private const Int64 Megabyte = Kilobyte * 1024; | |
private const Int64 Gigabyte = Megabyte * 1024; | |
private 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 step = Megabyte; | |
if (args.Length >= 1) { | |
step = int.Parse (args [0]); | |
} | |
var numThreads = 1; | |
if (args.Length >= 2) { | |
numThreads = int.Parse (args [1]); | |
} | |
Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}"); | |
Console.WriteLine($"Increment size: {step } bytes"); | |
Console.WriteLine($"Creating {numThreads} threads"); | |
for (var i = 0; i < numThreads; i++) | |
{ | |
var t = new Thread(Work) {Name = $"Eater#{i}"}; | |
t.Start(step); | |
} | |
while (true) | |
{ | |
Thread.Sleep(5000); | |
{ | |
Process currentProc = Process.GetCurrentProcess(); | |
Console.WriteLine ($"Process size: {StrSize(currentProc.PrivateMemorySize64)}"); | |
Console.WriteLine (); | |
} | |
} | |
} | |
private class Node { | |
public byte[] data; | |
public Node prev; | |
} | |
public static void Work (object param) | |
{ | |
Console.WriteLine($"Thread {Thread.CurrentThread.Name} started"); | |
var step = (Int64)param; | |
var start = DateTime.Now; | |
Node prev = null; | |
while(true) { | |
var data = new byte[step + (long)(Random.NextDouble() * step)]; | |
var node = new Node(); | |
node.data = data; | |
node.prev = prev; | |
prev = node; | |
for (var j = 0; j < data.Length; j += 10) { | |
data [j] = 10; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From other window: