Why does scopedTask increase memory consumption?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Nov 21 21:07:47 PST 2011


Magic uncovered, it's because the Task instantiates its own private task pool:

    void executeInNewThread() @trusted {
        pool = new TaskPool(basePtr);
    }

Ok, so I need to keep my own thread pool or use the global one and
then use task() to create a task and add it to the pool. I can do this
via:

taskPool.put(aTask);

But where on earth is the "start" method for taskPool? There's stop,
but I don't see start. I've temporarily made doSingleTask() public,
and calling it works perfectly (of course I do have to keep using
put() every time). So with those changes the memory issues go away.

If only we didn't have this awful GC then I wouldn't have to care
about these issues! :-)

On 11/22/11, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> import core.thread;
> import std.parallelism;
> import std.stdio;
>
> enum loops = 100;
>
> void runTask()
> {
>     static void test() { }
>     auto newTask = scopedTask(&test);
>     newTask.executeInNewThread();
>     newTask.yieldForce();
> }
>
> void main()
> {
>     foreach (_; 0 .. loops)
>         runTask();
>
>     writeln("done");
>     Thread.sleep(dur!"seconds"(4));
> }
>
> Running this consumes 8.024 KB on a win32 quad-core machine. If I set
> loops to 100_000, it consumes 9.040 KB. I know each thread has its own
> storage, but I'm invoking only one thread at a time, so after that
> thread finishes I would assume the thread would clean up after itself
> and release all memory.
>
> In another app using scopedTask keeps eating more and more memory,
> even though calling the same target function serially doesn't increase
> memory consumption.
>
> I admit I'm very new to concurrency so I don't know whether this is
> normal or not. I know each thread has its own TLS but afaik when a
> thread dies it should release those resources.
>


More information about the Digitalmars-d-learn mailing list