对象数组到字节数组 - Marshal.AllocHGlobal 碎片查询

发布于 2024-11-15 12:21:50 字数 1572 浏览 7 评论 0原文

我认为对 Fredrik Mörk这篇2年前的帖子,所以我想我应该把它作为一个新问题来问......

注意: 这并不是对答案的批评,我只是想在深入研究内存管理/元帅类之前理解这一切。

在该答案中,函数 GetByteArray 在循环内为给定数组中的每个对象分配内存。

上述帖子中的 GetByteArray 函数是否会从为提供的数组的总大小分配内存中受益:

Dim arrayBufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(<arrayElement>) * <array>.Count)

我只是想知道分配内存(如答案所示)是否会导致任何类型的碎片?假设可能存在碎片化,是否会产生很大的影响?以我展示的方式分配内存会强制您调用 IntPtr.ToInt## 来获取总体分配指针的指针偏移量,从而强制您检查底层体系结构以确保使用正确的方法* 1或者有更好的方法吗? (ToInt32/ToInt64 取决于 x86/64?)

*1 我在其他地方读到调用错误的 IntPtr.ToInt## 将导致溢出异常。我的意思是我会使用:

Dim anOffsetPtr As New IntPtr(arrayBufferPtr.ToInt## + (loopIndex * <arrayElementSize>))

我已经阅读了一些关于 VB.Net Marshal 类和内存分配的文章;下面列出了,但如果您知道任何其他好文章,我会洗耳恭听!

http://msdn.microsoft.com/en- us/library/system.runtime.interopservices.marshal.aspx

http://www.dotnetbips.com/articles/44bad06d-3662- 41d3-b712-b45546cd8fa8.aspx

到目前为止我最喜欢的: http://www.codeproject.com/KB/vb/Marshal.aspx

I didn't think it fair to post a comment on Fredrik Mörk's answer in this 2 year old post, so I thought I'd just ask it as a new question instead...

NB: This is not a critiscm of the answer in any way, I'm simply trying to understand this all before delving into memory management / the marshal class.

In that answer, the function GetByteArray allocates memory to each object within the given array, within a loop.

Would the GetByteArray function on the aforementioned post have benefited at all from allocating memory for the total size of the provided array:

Dim arrayBufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(<arrayElement>) * <array>.Count)

I just wonder if allocating the memory, as shown in the answer, causes any kind of fragmentation? Assuming there may be fragmentation, would there be much of an impact to be concerned with? Would allocating the memory in the way I've shown force you to call IntPtr.ToInt## to obtain pointer offsets from the overall allocation pointer, and therefore force you to check the underlying architecture to ensure the correct method is used*1 or is there a better way? (ToInt32/ToInt64 depending on x86/64?)

*1 I read elsewhere that calling the wrong IntPtr.ToInt## will cause overflow exceptions. What I mean by that statement is would I use:

Dim anOffsetPtr As New IntPtr(arrayBufferPtr.ToInt## + (loopIndex * <arrayElementSize>))

I've read through a few articles on the VB.Net Marshal class and memory allocation; listed below, but if you know fo any other good articles I'm all ears!

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx

http://www.dotnetbips.com/articles/44bad06d-3662-41d3-b712-b45546cd8fa8.aspx

My favourite so far:
http://www.codeproject.com/KB/vb/Marshal.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-11-22 12:21:50

可以为整个数组分配非托管内存,然后使用 SizeOf(arrayElement)*loopIndex 偏移量复制每个数组元素。最好根据当前平台使用适当的ToInt32/ToInt64方法,例如:

Dim anOffsetPtr 
if arrayBufferPtr.Size = 4 then
    anOffsetPtr  = New IntPtr(arrayBufferPtr.ToInt32() + (loopIndex * arrayElementSize)) 
else
    anOffsetPtr  = New IntPtr(arrayBufferPtr.ToInt64() + (loopIndex * arrayElementSize)) 
endif

It is possible to allocate unmanaged memory for the whole array, and then copy every array element with SizeOf(arrayElement)*loopIndex offset. It is better to use appropriate ToInt32/ToInt64 method, according to the current platform, like:

Dim anOffsetPtr 
if arrayBufferPtr.Size = 4 then
    anOffsetPtr  = New IntPtr(arrayBufferPtr.ToInt32() + (loopIndex * arrayElementSize)) 
else
    anOffsetPtr  = New IntPtr(arrayBufferPtr.ToInt64() + (loopIndex * arrayElementSize)) 
endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文