Skip to content

Memory leak with Arc / Orc for allocation inside threads  #23361

Open
@eagledot

Description

@eagledot

Description

Following code leads to a very high memory usage for memory allocated with either alloc or allocShared inside threads when arc / orc is used as mm option.

Using -d:useMalloc leads to expected behaviour !

when defined(windows):
    import std/winlean

var
   thr: array[2, Thread[tuple[a,b,: int]]]

proc threadFunc(interval: tuple[a,b: int]) {.thread.} =
    for i in interval.a..interval.b:
        let memory = allocShared(2048*4)
        
        # some work simulation.
        var sum = 0
        for i in 0..<2000:
            cast[ptr UncheckedArray[float32]](memory)[i] = float32(i)
        
        deallocShared(memory) # deallocate here.

proc some_function()=
    for i in 0..high(thr):
        createThread(thr[i], threadFunc, (i*10, i*10+5))
    joinThreads(thr)

    when defined(windows):
        for i in 0..high(thr):
            discard closeHandle(cast[Handle](thr[i].sys))

for i in 0..<1000000:
    some_function()

Nim Version

Nim Compiler Version 2.0.2 [Windows: amd64]
Compiled at 2023-12-15
Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release

(Same behaviour with nim 1.6.8)

Current Output

No response

Expected Output

Memory should be freed as expected when using `dealloc` or `deallocShared`.

Possible Solution

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions