Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-02-07
14:47 • Closed ticket [5888e0e101]: memory leak plus 6 other changes artifact: c7ef5416b6 user: sebres
13:30 • Ticket [5888e0e101]: 3 changes artifact: fa52f8f9cb user: pointsman
12:41 • Ticket [5888e0e101]: 3 changes artifact: 1b50e24570 user: oehhar
12:40 • Ticket [5888e0e101]: 3 changes artifact: 76d3f88e29 user: wmrieker
12:38 • New ticket [5888e0e101]. artifact: 9772f83c48 user: wmrieker

memory leak

Closed / Invalid5888e0e1018d10cbac197161f9b415c532f1b6f6Created 299 days agoUpdated 299 days ago
Descriptionplain2026-02-07 12:38:59
Running the attached script, it goes slower and slower.  And it grows in size.

I am running it on Linux Ubuntu 22.
wmriekerplain2026-02-07 12:40:57
set randseed 123456789

for {set i 0} true {incr i} {

    set randval 0
    for {set j 0} {$j < 12} {incr j} {
        set xnor [expr {~ (($randseed >> 31) ^ ($randseed >> 30) ^ ($randseed >> 28) ^ ($randseed >> 27))}]
        set randseed [expr {($randseed << 1) | ($xnor & 1)}]
        set randval [expr {($randval << 1) | ($randseed & 1)}]
    }

    set r $randval
    if {$i % 1024 == 0} {
        set f [open /proc/self/status r]
        set s ""
        while {[gets $f l] >= 0} {
            if {[string range $l 0 6] == "VmSize:"} {
                set s $l
                break
            }
        }
        close $f
        set t [clock seconds]
        puts [format "%02d:%02d:%02d  %12d  %04o %s" [expr {$t / 3600 % 24}] [expr {$t / 60 % 60}] [expr {$t % 60}] $i $r $s]
    }
}
oehharplain2026-02-07 12:41:13
Sorry, there is no attachement...
Harald
pointsmanplain2026-02-07 13:30:52
The value of the variable randseed is constantly growing - after a few thousand
runs of the outer loop it already has a length of a few ten thousand digits.

Given this the growing runtime and memory allocation is basically expected.

At least in the given form the script doesn't clearly show there is memory lost.
sebreswiki2026-02-07 14:47:40

Yes, the analyse of Rolf is pretty correct - the (bignum) value of randseed grows endless by 2i with each iteration, so the bignum math becomes slower as well as the memory reserved for this "integer" grows.

I suspect it's missing something like a constant constraint (truncation to wide-int or int):

 set randval 0
 for {set j 0} {$j < 12} {incr j} {
   set xnor [expr {~ (($randseed >> 31) ^ ($randseed >> 30) ^ ($randseed >> 28) ^ ($randseed >> 27))}]
-  set randseed [expr {($randseed << 1) | ($xnor & 1)}]
+  set randseed [expr {($randseed << 1) & 0x7fffffffffffffff | ($xnor & 1)}]
   set randval [expr {($randval << 1) | ($randseed & 1)}]
 }

Thus it is clearly not a memleak or performance degradation, ticket is closed as incorrect.

Log in (anonymous is fine) to add a comment.