본문 바로가기
C#

[C#] .NET 6 - NativeMemory 사용

by Jcoder 2022. 1. 12.
static unsafe void Test1()
{
    int* age = (int*)System.Runtime.InteropServices.NativeMemory.Alloc(sizeof(int));
    int* ages = (int*)NativeMemory.Alloc(2, sizeof(int));
    try
    {
        *age = 28;
        Console.WriteLine($"{*age}");

        ages[0] = 29;
        ages[1] = 30;

        for (int i = 0; i < 2; i++)
        {
            Console.WriteLine($"{i} : {ages[i]}");
        }
    }
    finally
    {
        NativeMemory.Free(age);
        NativeMemory.Free(ages);
    }
}

 

NativeMemory Class (System.Runtime.InteropServices) | Microsoft Docs

 

NativeMemory Class (System.Runtime.InteropServices)

This class contains methods that are mainly used to manage native memory.

docs.microsoft.com

 

참고 : .NET Framework: 866. C# - 고성능이 필요한 환경에서 GC가 발생하지 않는 네이티브 힙 사용 (sysnet.pe.kr)

 

.NET Framework: 866. C# - 고성능이 필요한 환경에서 GC가 발생하지 않는 네이티브 힙 사용

글쓴 사람 정성태 (techsharer at outlook.com) 홈페이지 첨부 파일 부모글 보이기/감추기 C# - 고성능이 필요한 환경에서 GC가 발생하지 않는 네이티브 힙 사용 지난 발표에서, .NET Conf 2019 Korea - "닷넷 17

www.sysnet.pe.kr