Jonny
Jonny

Reputation: 2927

TPL Dataflow Resources not released

I have a flow setup in the following way:

_publisherQueue = CreateBuffer();
var batchingBlock = CreateBatchBlock(options.BatchSize);
var debounceBlock = CreateDebounceBlock(options.DebounceInterval, batchingBlock.TriggerBatch);
var publishBlock = CreatePublishBlock();
var groupByTopicBlock = CreateGroupByTopicBlock(publishBlock);

_publisherQueue.LinkTo(debounceBlock, new DataflowLinkOptions { PropagateCompletion = true});
debounceBlock.LinkTo(batchingBlock, new DataflowLinkOptions { PropagateCompletion = true });
batchingBlock.LinkTo(groupByTopicBlock, new DataflowLinkOptions { PropagateCompletion = true });

where:

I cannot dispose the links because this flow should live for the entire life time of the program (in this case it is a Windows Service).

I have noticed that every time I invoke _publisherQueue (which is a BufferBlock) some memory is used, which is normal, However after the process is finished the memory allocated is not being released.

This is worrisome due to the fact that this is a long running process that will accept inputs at random intervals.

It's my first attempt at using TPL so most probably I am not doing proper disposal. However I'm not sure what I need to dispose of since I need these structures to remain alive throughout the life time of the program.

Upvotes: 4

Views: 1048

Answers (1)

VMAtm
VMAtm

Reputation: 28355

I have concerns about this part:

CreateGroupByTopicBlock returns an ActionBlock whose Action triggers the Action block returned by CreatePublishBlock

Looks like a closure here, which easily can lead to memory leaks, as it's being compiled into a internal class, with storing all the references from it in fields. You should investigate your application with some memory profiler (either built-in VS profiler or some external, like dotTrace) and see if there are any objects being held by reference inside this closure, and, may, rewrite your logic to avoid unnecessary closures in your code.

Upvotes: 2

Related Questions