Oh, thanks Araq for pointing me on the problem in code. I not expected that 
language inventor will review it. Otherwise I would prepare much better! So 
regarding the GC, does it mean, that even with --d:malloc the allocator will 
hold that memory until whole process quits? Along with that mistake in code, I 
forgot also mention that I specified this parameter in nim.cfg If so, I really 
don't understand what to do with it? Switch to manual memory management? What 
options we can have when program needs to use actively ram, and alloc/dealloc 
objects with immediate real free()? I'm not very experienced in different Gc's 
and their working principles, but I thought GC will use the ram optimally. But 
with current example, such executable can hold a lot of ram on the server, 
while other heavy executable may need it. For example until the exe will not 
save processed data on the disk, all that GigBytes will be hold.

Here is updated code, with corrected Thread variable declaration. Also I 
removed unnecessary stuff, to not confuse others with my unique coding style :)
    
    
    import
      tables,
      os
    
    # Lock, Shared ram Table, thread input params(attributes), and thread var.
    var
      theTable = createShared(OrderedTable[string, seq[string]], 1)
      thread: array[1, Thread[ptr OrderedTable[string, seq[string]]]]
    
    # Thread procedure. Just fot test it stores in Dictionary the key and 
sequence of strings with same value.
    proc theProc(tbl: ptr OrderedTable[string, seq[string]]): void {.thread.} =
        var
          labels = @["Black Demon", "Turtoise", "Bill Torvalds", "BUG", "Black 
Demon", "Bill Torvalds", "Linus Gates", "Linus Gates", "Linus Gates"]
        
        for i in labels.items:
          
          if not tbl[].hasKey(i):
            tbl[][i] = @[]
          var str: string = i
          tbl[][i].add str
        # Emptying the sequences. I expect Collector will free them
        labels = @[]
    
    
    
    
    # Creating the thread
    createThread[ptr OrderedTable[string, seq[string]]](thread[0], theProc, 
theTable)
    
    # Awaiting to finish thread execution
    joinThreads(thread)
    
    # Calculating Table contents size, and printing out content of theTable
    var size: int = 0
    debugEcho "\n Printing result from main():"
    for key, sequence in theTable[].pairs:
      size.inc(key.len)
      for i in sequence:
        size.inc(i.len)
      debugEcho "- ", key, sequence
    
    if not running(thread[0]):
      freeShared(theTable)
      GC_fullCollect()
      debugEcho "--------------------------------------------"
      debugEcho "Size of theTable content is: ", size, ". But the ram were not 
freed by collector. See Resident (RES) in linux 'top' for example."
      sleep(120000)
    else:
      debugEcho "Thread still running, impossible scenario."
    
    
    
    Run

Reply via email to