Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 9ba84f713faa12071d57be52d123edd353d7a63e
Ticket: 218378ffffffffffffffffffffffffffffffffff
memory leak in regsub
User & Date: nobody on 2000-10-26 00:56:52
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 4333 Bug
    Version: 8.3
    SubmitDate: '2000-03-07'
    LastModified: '2000-03-21'
    Severity: SER
    Status: Closed
    Submitter: techsupp
    ChangedBy: ericm
    OS: All Unix
    FixedDate: '2000-10-25'
    ClosedDate: '2000-03-21'
    
    
    Name:
    Dave Dykstra
    
    ReproducibleScript:
    I kept paring down my code to the following test script.  While running tclsh, in another
    window do a shell loop running ps -l on the process every second or two to watch the process
    size expansion.  It does not happen with tclsh8.0.  I have observed the growth on solaris, hp, and
    amdahl's UTS.  The "after"s are in there to allow time for ps to note the process size.
    
    -----------------------
    after 3000
    puts "initializing"
    lappend subs {^lib\/toolman\/.*}
    lappend subs {^adm\/rug\/local\/.*}
    lappend subs {^\.[^\/]*_time$}
    lappend subs {^\.nsb_[^\/]*$}
    
    for {set n 0} {$n < 13} {incr n} {
        foreach sub $subs {
                lappend subs "^x/[string range $sub 1 end]"
        }
    }
    
    puts "done with setting up subs"
    after 3000
    puts "calling regsub on 'nonmatch' with [llength $subs] substitutions"
    set path "nonmatch"
    foreach sub $subs {
            regsub -all -- $sub $path "" path
    }
    puts "path is still $path"
    after 3000
    puts "done"
    
    ObservedBehavior:
    Process size grows.
    
    
    
    Memory management in Tcl is a bit of a tricky subject.  One important bit (important for this discussion anyway) is that all literals are stored in a global literal table.  This enables better performance at the cost of more memory usage.
    
    Another important bit has to do specifically with regular expressions.  When a regular expression is created, it has to be "compiled" from the string representation of it.  This can be compute-intensive, so in order to further enhance performance, the compiled representation of a regular expression is cached, so if the regexp is used again, Tcl can avoid recompiling it.
    
    I believe that what you are seeing here is the growth of the global literal table, and the creation of many compiled regular expression objects.  Here's a breakdown of what is happening:
    
    1.  Each lappend subs creates an entry in the global literal table.  This
    entry is a string object.  This causes some memory allocation.
    
    2.  Each time you call regsub with one of those literals, the object in the global literal table that is associated with it gets changed to a regexp object.  This causes some additional memory allocation.
    
    If you were to later go back and force the objects associated with those literals to be converted to some non-regular expression object (for example, by doing [llength] on it), the compiled regular expression bits would be freed.  Be aware that if you try this you may not actually see the memory footprint of your application decrease; although Tcl is freeing the memory, the OS may not actually reclaim it until it needs to.  This was the case on my Linux machine.
    
    In short, this is the expected behavior of the interpreter.  The process is growing because the global literal table is growing.  You can try to avoid this by avoiding literal expressions and instead using computed values.
    
    - eric
    
    -- 03/21/2000 ericm
    
    
    
  4. foundin changed to: "obsolete: 8.3"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "69. Other"
  10. title changed to: "memory leak in regsub"
  11. type changed to: "Bug"