Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Runtime.CompilerServices;
namespace GCTest
{
class Program
{
static unsafe void Main(string[] args)
{
ref var refElm = ref AllocateArray();
Console.WriteLine("0x{0:x16}", (IntPtr)Unsafe.AsPointer(ref refElm));
ref var structElm = ref Unsafe.As<int, FooStruct>(ref refElm);
Console.WriteLine("0x{0:x16}", (IntPtr)Unsafe.AsPointer(ref structElm));
GC.Collect();
Console.WriteLine("0x{0:x16}", (IntPtr)Unsafe.AsPointer(ref refElm));
Console.WriteLine("0x{0:x16}", (IntPtr)Unsafe.AsPointer(ref structElm));
}
static ref int AllocateArray()
{
var rng = new Random();
const int arrayCount = 100000;
const int elementCount = 100;
var arrays = new int[arrayCount][];
for (var i = 0; i < arrayCount; i++)
{
arrays[i] = new int[elementCount];
}
return ref arrays[rng.Next(1, arrayCount)][rng.Next(elementCount)];
}
}
struct FooStruct
{
public int V;
}
}
/*
0x2633590243804
0x2633590243804
0x2633558576356
0x2633558576356
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment