开发者

Does pinning an object in the LOH affect GC performance?

I've read that pinning objects in the manage开发者_StackOverflow中文版d heap affects the GC performance in .NET, because the GC can't compact memory if there are pinned objects "in the way". But since the large object heap isn't compacted anyway, this shouldn't apply to objects in the LOH. Are there any other hidden costs of pinning an object that's in the LOH? Or can I safely pin objects in the LOH without degrading GC performance?


Well, just because the Large Object Heap (LOH) isn't compacted doesn't mean that it's not collected. The LOH is collected and pinning an object there will have ramification on future allocations.

Because an object is pinned, it effectively shrinks the amount of memory that is available in the LOH (the same as if you were holding a reference). When another request to allocate a large object is made, if there are too many pinned/held references in the LOH, you can run into issues allocating more large objects.

When a mark is done during the mark-and-sweep part of garbage collection, the CLR probably marks all references that are pinned as roots so there's probably no impact during this part of collection; it would behave the same way if someone kept a reference to the large object.

Since deallocation happens the same way on the LOH (the block is simply tagged as being available), this operation isn't impacted either.

And finally, since the LOH is not compacted, this operation never takes place on this heap during a GC, so this isn't impacted here.

In summary, allocations on the LOH can definitely be impacted by pinning references to objects on the LOH, while collections on the LOH are most likely not.

Although let's not forget that allocating and holding large blocks of memory can have ramifications on systems in general, these comments are strictly about the LOH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜