You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Audit usages of Marshal.AllocHGlobal for ones that can be replaced with NativeMemory.Alloc #54297
#54006 introduced new NativeMemory APIs that wrap malloc, calloc, realloc, and aligned_alloc. We should audit existing usages of Marshal.AllocHGlobal for cases where it can be safely replaced with malloc.
Notably cases like StringToPtrHGlobal do not qualify as they are documented to be a call to LocalAlloc on Windows. However, on Unix and in cases where this is not a definitive contract, it should be safe to replace these calls with faster ones to NativeMemory.
It is not worth to fix legacy libraries like System.Speech, but it would be nice to get rid of the dependency on the AllocHGlobal/LocalAlloc in core scenarios to benefit from the cleanup.
I think a reasonable goal to shoot for is that the binary published by dotnet new webapiaot does not import LocalAlloc Windows API (or understand where the dependency is required).
Notably cases like StringToPtrHGlobal do not qualify as they are documented to be a call to LocalAlloc on Windows. However, on Unix and in cases where this is not a definitive contract, it should be safe to replace these calls with faster ones to NativeMemory.
That can be solved as well for Windows if NativeMemory could get the LocalAlloc case as well, but fallback to malloc on non-Windows or w/e that way the implementation can be simplified.
Activity
reflectronic commentedon Jun 18, 2021
I'll take this one.
tannergooding commentedon Jun 18, 2021
Thanks reflectronic. Assigned out.
jkotas commentedon Oct 12, 2025
There is still a bunch left that Copilot missed.
It is not worth to fix legacy libraries like System.Speech, but it would be nice to get rid of the dependency on the AllocHGlobal/LocalAlloc in core scenarios to benefit from the cleanup.
I think a reasonable goal to shoot for is that the binary published by
dotnet new webapiaotdoes not importLocalAllocWindows API (or understand where the dependency is required).dumpbin /imports test.execan be used to check whetherLocalAllocimport is present. https://github.com/MichalStrehovsky/sizoscope can be used to find where it is coming from.AraHaan commentedon Feb 17, 2026
That can be solved as well for Windows if
NativeMemorycould get theLocalAlloccase as well, but fallback tomallocon non-Windows or w/e that way the implementation can be simplified.tannergooding commentedon Feb 17, 2026
LocalAllocis a "legacy" API that is no longer recommended for use, there is no desire to expose a new API that gives access to it.dotnet-policy-service commentedon Apr 13, 2026
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.
Use NativeMemory instead of AllocHGlobal in WindowsIdentity and Event…