Skip to content

Audit usages of Marshal.AllocHGlobal for ones that can be replaced with NativeMemory.Alloc #54297

Description

@tannergooding

#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.

Activity

  1. ghost added
    untriaged
    on Jun 16, 2021
  2. added
    help wanted
    and removed on Jun 16, 2021
  3. reflectronic commented on Jun 18, 2021

    @reflectronic
    Contributor

    I'll take this one.

  4. tannergooding commented on Jun 18, 2021

    @tannergooding
    MemberAuthor

    Thanks reflectronic. Assigned out.

  5. removed
    untriaged
    on Jun 18, 2021
  6. added this to the Future milestone on Jun 18, 2021
  7. ghost added
    in-pr
    on Jun 21, 2021
  8. ghost removed
    in-pr
    on Jun 30, 2021
  9. added
    in-pr
    on Oct 11, 2025
  10. jkotas commented on Oct 12, 2025

    @jkotas
    Member

    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 webapiaot does not import LocalAlloc Windows API (or understand where the dependency is required).

    dumpbin /imports test.exe can be used to check whether LocalAlloc import is present. https://github.com/MichalStrehovsky/sizoscope can be used to find where it is coming from.

  11. reopened this on Oct 12, 2025
  12. removed
    in-pr
    on Oct 12, 2025
  13. AraHaan commented on Feb 17, 2026

    @AraHaan
    Member

    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.

  14. tannergooding commented on Feb 17, 2026

    @tannergooding
    MemberAuthor

    LocalAlloc is a "legacy" API that is no longer recommended for use, there is no desire to expose a new API that gives access to it.

  15. dotnet-policy-service commented on Apr 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Interop-coreclrhelp wanted

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions