Is there a way to store managed pointers in a stackalloc buffer without incurring unexpected behavior from the GC? [duplicate]

I’m working with C# in a game engine and frequently need to pass around subsets of collections of objects to different synchronous functions. I need to avoid making heap allocations for each call, and have investigated a few solutions. One that seemed elegant conceptually was to stackalloc a buffer and store references to the objects in it, then pass that buffer as a Span to whatever function I needed to call.

However, I know the docs state that stackalloc can only be used with unmanaged types. I’ve read a few descriptions of why this is the case, including this SO answer. My limited understanding is that the garbage collector needs to track all of the potential references to objects for the purposes of moving and freeing them. But stackalloc creates a value type with no fields, just a raw blob of memory with no type info, so the GC inherently can’t know anything about it.

Through use of the Unsafe and MemoryMarshal classes, I’ve written a workaround that allocates and reinterprets a buffer on the stack:

IntPtr* ptr = stackalloc IntPtr[count];
ref T tRef = ref Unsafe.AsRef<T>(ptr); // T is some object
Span<T> buff = MemoryMarshal.CreateSpan(ref tRef, count);
... // Copy references into buff and pass it to some function

This works as expected so far. However, I haven’t used this solution extensively yet and don’t know if there will be any unexpected behavior over sustained GC runs.

I know the objects won’t be freed as they’re still referenced by the original heap allocated collections. But I want to ensure the objects won’t be moved by the GC while calling a function with these stack buffers. Is this possible?

Will the GC account for the references stored in this memory because it’s now being pointed to by a Span? If not, is it possible to avoid the referenced objects from being moved using the Span with the fixed statement and pinning them? If that won’t work, is there any other way to ensure they won’t be moved for the duration of the function call, or is this just not possible whatsoever?

New contributor

Lich is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

6

I am explicitly attempting to avoid allocating a new heap array for
each call I make.

For your purposes, there seems to be a built-in solution – ArrayPool<T>:

Using the ArrayPool class to rent and return buffers (using the
Rent and Return methods) can improve performance in situations where
arrays are created and destroyed frequently, resulting in significant
memory pressure on the garbage collector.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật