Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created June 19, 2023 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FeepingCreature/4a4f688cd12ba5fabad6cac924b2f6e4 to your computer and use it in GitHub Desktop.
Save FeepingCreature/4a4f688cd12ba5fabad6cac924b2f6e4 to your computer and use it in GitHub Desktop.
module test;
import core.memory : GC;
import core.sync.semaphore;
import core.thread;
import std;
static string[] keys = [
"foo", "bar", "baz", "whee",
"foo1", "foo2", "foo3", "foo4", "foo5", "foo6", "foo7", "foo8",
"bar1", "bar2", "bar3", "bar4", "bar5", "bar6", "bar7", "bar8",
"baz1", "baz2", "baz3", "baz4", "baz5", "baz6", "baz7", "baz8"];
void spamalot(Semaphore sema, int i) {
auto rnd = Random(i);
int rand() => uniform(0, int.max, rnd);
void recursionTest(int depth) {
int r = rand();
if (depth < 1000) {
recursionTest(depth + 1);
if (rand() % 300 == 0)
recursionTest(depth + 1);
}
string[string] assocArray;
static foreach (_; 20.iota)
{
assocArray[keys[rand() % $].idup] = keys[rand() % $];
}
}
recursionTest(0);
sema.notify;
Thread.sleep(3600.seconds);
}
void main() {
Thread[] threads;
auto sema = new Semaphore(0);
enum threadCount = 100;
threadCount.iota.each!((i) {
auto thread = new Thread({ spamalot(sema, i); });
thread.start;
threads ~= thread;
});
// this clears the leak up.
// threads.each!((thread) { thread.join; });
threadCount.iota.each!((_) {
sema.wait;
});
GC.collect;
GC.minimize;
// Now look at residential memory for the process.
Thread.sleep(3600.seconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment