Created
September 29, 2017 12:33
-
-
Save azyobuzin/e6efc98e69e585dd9f86ec61734d6d76 to your computer and use it in GitHub Desktop.
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.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