Dear all. After hours of research and googling for possible solution I cannot
find the answer. So here goes my first post on this forum. I need to have
memory vise optimized executable, but with use of dynamic MM. Here below is
shortened version of similar program structure. The difference is that it not
does silly things like copy and useless data processing:
import
tables,
os
# Definition of nput params for the Thread
type
param = tuple[tbl: 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(attr: param) {.thread.} =
var
labels = @["Black Demon", "Turtoise", "Bill Torvalds", "BUG", "Black
Demon", "Bill Torvalds", "Linus Gates", "Linus Gates", "Linus Gates"]
strings: ref seq[string] = new(seq[string])
`=copy`(strings[], labels)
for i in strings[]:
# debugEcho i
if not attr.tbl[].hasKey(i):
attr.tbl[][i] = @[]
var str: string = newStringOfCap(i.len)
`=copy`(str, i)
attr.tbl[][i].add str
# Emptying the sequences. I expect Collector will free them
labels = @[]
strings[] = @[]
proc main(): void =
# Shared ram Table, thread input params(attributes), and thread var.
var
theTable = createShared(OrderedTable[string, seq[string]], 1)
attr = ( tbl: theTable)
thread = Thread[param]()
# Creating the thread
createThread(thread, theProc , attr)
# Awaiting to finish thread execution
thread.joinThreads()
# 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):
freeShared(theTable)
GC_fullCollect()
debugEcho "--------------------------------------------"
debugEcho "Size of theTable content is: ", size, ". But resources were
not freed by the collector. See Resident (RES) in linux 'top' for example."
sleep(120000)
else:
debugEcho "Thread still running, impossible scenario."
main()
Run
My results with use of different GC:
arc: 786 bytes
orc: 892 bytes
boehm: 2410 bytes
regions: 904 bytes
Run
While size of resulting Table content is: 138 bytes. I know resident data in
ram contains stack too. But in case of huge data processing, usage goes to
GigaBytes of not freed ram, until process will not quit. How to deal with it?