I wanted to see how long nim held on to unused memory and I wrote a simple 
program that assigns a new seq to the same variable in a loop that runs twice 
every second. The seq needs under 200k but every once in a while getTotalMem 
reports double the previous high. After a while it gets up to over 8 billion on 
a 16 GB system arch linux system. It goes back down after a few more loops but 
it again doubles after a while. I wonder if nim really tried to grab that much 
memory for a short while. The follwing program shows the problem in a few 
minutes.
    
    
    import os
    
    proc fmt(x:int):string =
        var s = $x
        var ll = s.len
        var j,l2:int
        result = newstring(ll + (ll-1) div 3)
        for i in 0..<ll:
            result[j] = s[i]
            inc j
            if (ll-i) mod 3 == 1 and (ll-i)>1:
                result[j] = ','
                inc j
    
    proc mtest() =
        var imax = 0
        while true:
            var n = 20000
            var v=newseq[int](n)
            for i in 0..<n: v[i]+=i
            sleep(500)
            var t = getTotalMem()
            if t > imax: imax = t
            echo "Total = ", t.fmt
            echo "Free  = ", getFreeMem().fmt
            echo "Occu  = ", getOccupiedMem().fmt
            echo "Max   = ", imax.fmt
            echo v[1] + v[2], "\n"
    
    
    when isMainModule:
        mtest()
    
    

Reply via email to