WiseGuyEh WiseGuyEh - 27 days ago 10
C# Question

What's happens when a Task continuation is scheduled on a different processor (GC logical heaps)?

As I understand it, when the Server GC-mode is enabled for a .NET application it allocates a logical heap per processor, where threads scheduled on each processor will allocate memory to.

What happens to the memory allocated to a

Task
with multiple awaits? It could potentially be executed on any Thread from the ThreadPool, so could jump between logical heaps.

Does memory allocated on a previous logical heap get copied to the current executing logical heap? Is there any cost to not executing
Task
continuations on the same logical heap/core?

Answer

What happens to the memory allocated to a Task with multiple awaits? It could potentially be executed on any Thread from the ThreadPool, so could jump between logical heaps.

A thread has visibility to the entire address space, regardless of the logical separation. Heap objects can point to different objects in another heap without a boundary. Keep in mind that different threads can still be scheduled on the same CPU in different time slices.

Does memory allocated on a previous logical heap get copied to the current executing logical heap? Is there any cost to not executing Task continuations on the same logical heap/core?

No, that would incur a tremendous run time performance hit if the runtime was forced to copy objects between heaps. Again, the fact that a thread may not run on the same logical heap that allocated it doesn't mean it can't reference that heap. The idea behind a heap per core is to be able to scale allocations and be able to imorove parallel gc collections.

Comments