Question regarding GC notification! #132
-
Hey dude, B4 I do begin asking, i am extremely astonished in what you have created with this library!! Very big congrats to you and wish ya all the best and luck with this project and life ! I had to say that b4 cause its litterally (next) in terms of what .net can offer . To my question: Is there any chance to get notified from the GC when:
Any idea on this much appreciated! Cheers! |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 12 replies
-
@Shpendicus , thanks for your feedback! GC actually compacts segments related to ephemeral generations (0 and 1). It can't compact the array. If it 20 elements long, it will stay the same during the lifetime of the array. GC can move the whole object in the memory. Additionally, there is no way to measure exact size of by-ref object (class) in the memory, because GC may add padding when object is moving from one generation to another. P.S.: GC always apply compaction when moving objects from Gen0 to Gen1 and from Gen1 to Gen2. Gen2, by default, is not a subject of compaction, but it can be triggered explicitly from code. |
Beta Was this translation helpful? Give feedback.
-
@sakno thx for ur reply! My main point only is actually that he does not change the distance of the fields from Item0 to ItemX, like for isntance if you do: nint distance = ref Unsafe.ByteOffset(ref first, ref other); //----------->what will this be AFTER he has moved the entire object somewhere and ahs maybe even compressed ir or whatever it does to it. |
Beta Was this translation helpful? Give feedback.
-
There is no way to predict how the objects referenced by the array elements are placed in the memory. So the distance can be any. |
Beta Was this translation helpful? Give feedback.
-
If you really care about data locality (due to performance reasons), convert |
Beta Was this translation helpful? Give feedback.
-
Hmm, i cant do that cause i am building a datastructure as a wrapper for other datastructures/types so i kinda have to work on what I do get and i dont wanna constraint that, cause it should work (semtantically) for both. And it is okay when the distance is any value, the main criteria only is that the distance does not change randomly at runtime!! This I ask you, if that can happen, or is the layout fixed once he created the entire array? |
Beta Was this translation helpful? Give feedback.
-
Also it is OKAY if he compacts, but i would love to get notified for this! So, either:
|
Beta Was this translation helpful? Give feedback.
-
Ok, I got it. But GC doesn't change the layout of array elements. Address of each array element is aligned in memory according to its size. For ref types (class), each element is always aligned to To detect heap compaction, you can do the following: await GCNotification.HeapCompaction().WaitAsync(); |
Beta Was this translation helpful? Give feedback.
-
ahh thx mate good to know |
Beta Was this translation helpful? Give feedback.
ahh thx mate good to know