TPL Dataflow block consumes all available memory
You seem to misunderstand how TPL Dataflow works.
BoundedCapacity
limits the amount of items you can post into a block. In your case that means a single char
into the TransformManyBlock
and single string
into the ActionBlock
.
So you post a single item to the TransformManyBlock
which then returns 1024*1024
strings and tries to pass them on to the ActionBlock
which will only accept a single one at a time. The rest of the strings will just sit there in the TransformManyBlock
's output queue.
What you probably want to do is create a single block and post items into it in a streaming fashion by waiting (synchronously or otherwise) when it's capacity is reached:
private static void Main()
{
MainAsync().Wait();
}
private static async Task MainAsync()
{
var block = new ActionBlock<string>(async item =>
{
Console.WriteLine(item.Substring(0, 10));
await Task.Delay(1000);
}, new ExecutionDataflowBlockOptions { BoundedCapacity = 1 });
foreach (var item in GetSequence('A'))
{
await block.SendAsync(item);
}
block.Complete();
await block.Completion;
}