Finally I could locate the problem. It seems that sequence delete() proc does not really free memory. I wrote an example:
import os,times,sequtils
type
BigIntArray = array[0..10000, int]
var x = newSeq[BigIntArray]()
while(true) :
echo "Enter OccupiedMem: ", getOccupiedMem()
for i in 0..5000 :
x.add((new BigIntArray)[])
echo "new OccupiedMem: ", getOccupiedMem()
echo x.low,"..",x.high
x.delete(x.low,x.high)
echo "deleted OccupiedMem: ", getOccupiedMem()
echo x.low,"..",x.high
sleep(10000)
The RAM usage is constantly growing running this example. Probably, the sequence members are never destroyed by GC. How can I force GC to destroy them?
I use this 'highly non-idiomatic code' just to show the problem. In my "real life" program there is an object at the place of BigIntArray:
type
PTimeInfo = ref TimeInfo
TimeInfoEvent = ref object of PTimeInfo
channel : int
command : string
...
proc newTimeInfoEvent() : TimeInfoEvent =
new result
result.initTimeInfoEvent()
...
var arrSchedTimeInfoEvent = newSeq[TimeInfoEvent]()
...
arrSchedTimeInfoEvent.add(newTimeInfoEvent(arrSchedEvt[i].channel,arrSchedEvt[i].command))
Is there the same problem?I see similar issues in my progs.
Try to compile with nim c --threads:on --gc:boehm
this may show where the issue is .
Like in your first example , which I call gcT1.nim I get this :
Error: execution of an external compiler program 'gcc -c -w -I/data4 /NimCompiler/Nim/lib -o /data4/NimStuff/nimcache/test_gcT1.o /data4/NimStuff/nimcache/test_gcT1.c' failed with exit code: 256
/data4/NimStuff/nimcache/test_gcT1.c: In function ‘gcT1Init000’:
/data4/NimStuff/nimcache/test_gcT1.c:253:39: error: incompatible types when assigning to type ‘Bigintarray92005’ from type ‘NI *’
x_92052->data[x_92052->Sup.len] = LOC8;
Is there the same problem?
I'm afraid that's actually a different problem then.
Cool :) The real program has 1000+ lines and needs some external hardware to work. I listed the code supposed to have memory leak problem in my second code fragment. The only function lost is here:
proc initTimeInfoEvent(tie : TimeInfoEvent, ch : int = 0, cmd : string = "") =
var ti=getLocalTime(getTime())
tie.channel = ch
tie.command = cmd
tie.second = ti.second
tie.hour = ti.hour
tie.minute = ti.minute
tie.monthday = ti.monthday
tie.month = ti.month
tie.year = ti.year
tie.weekday = ti.weekday
tie.yearday = ti.yearday
tie.isDST = ti.isDST
tie.tzname = ti.tzname
tie.timezone = ti.timezone
I can prepare a small working example with wrapper if something is still not clear.I can prepare a small working example with wrapper if something is still not clear.
Yes, please do so. Example does not have to be minimal, but it really helps if it has no dependencies so that I can just add it as a test case to the suite.
For the moment I cannot reproduce the problem on test example :(
BTW, the real program compiled with --gc:markAndSweep switch still turns after three days and the memory consumption seems to be stable. And it segfaults immediately, compiled with --gc:boehm. Strange, isn't it?