Large Arrays, and LOH Fragmentation. What is the accepted convention?

后端 未结 6 1487
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 20:25

I have an other active question HERE regarding some hopeless memory issues that possibly involve LOH Fragmentation among possibly other unknowns.

What my question no

6条回答
  •  忘掉有多难
    2020-12-05 20:43

    You get it wrong. You do NOT need to havean array size 4000000 and you definitely do not need to call the garbace collector.

    • Write your own IList implementation. Like "PagedList"
    • Store items in arrays of 65536 elements.
    • Create an array of arrays to hold the pages.

    This allows you to access basically all your elements with ONE redirection only. And, as the individual arrays are smaller, fragmentation is not an issue...

    ...if it is... then REUSE pages. Dont throw them away on dispose, put them on a static "PageList" and pull them from there first. All this can be transparently done within your class.

    The really good thing is that this List is pretty dynamic in the memory usage. You may want to resize the holder array (the redirector). Even when not, it is about 512kbdata per page only.

    Second level arrays have basically 64k per byte - which is 8 byte for a class (512kb per page, 256kb on 32 bit), or 64kb per struct byte.

    Technically:

    Turn int[] into int[][]

    Decide whether 32 or 64 bit is better as you want ;) Both ahve advantages and disadvantages.

    Dealing with ONE large array like that is unwieldely in any langauge - if you ahve to, then... basically.... allocate at program start and never recreate. Only solution.

提交回复
热议问题