Skip to content
Tcl Source Code History Of Ticket 218378
Login

History Of Ticket 218378

Artifacts Associated With Ticket 218378

  1. Ticket change [9ba84f713f] (rid 111165) by nobody on 2000-10-26 00:56:52:

    1. assignee initialized to: "nobody"
    2. cmimetype initialized to: "text/plain"
    3. comment initialized 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 initialized to: "obsolete: 8.3"
    5. priority initialized to: "5 Medium"
    6. resolution initialized to: "None"
    7. status initialized to: "Open"
    8. submitter initialized to: "nobody"
    9. subsystem initialized to: "69. Other"
    10. title initialized to: "memory leak in regsub"
    11. type initialized to: "Bug"
  2. Ticket change [0660548d60] (rid 89216) by welch on 2000-10-26 08:21:16:

    1. closedate changed to: "2000-10-26T01:21:16"
    2. closer changed to: "welch"
    3. priority changed to: "2"
    4. resolution changed to: "Fixed"
    5. status changed to: "Closed"
  3. Ticket change [bc1810f8a9] (rid 129601) by dgp on 2001-04-20 01:59:01:

    1. closedate changed to: "2001-04-19T18:59:01"
    2. closer changed to: "dgp"
    3. subsystem changed to: "41. Memory Allocation"
  4. Ticket change [eb11e7f3bf] (rid 103472) by rheller on 2002-02-09 03:46:13:

    1. icomment:
      Logged In: YES 
      user_id=259412
      
      I have discovered that the http V2.0 package seems to suffer
      from this form of memory leak -- it is using *computed*
      patterns for regexp.  I have a script that is using the http
      package as part of an "endless" loop -- I have implemented a
      daemon type of program that fetches an image via HTTP from a
      web server (an embedded web server in a network camera)
      periodically to disk space on a file server.
      
      This deamon is showing a memory leak.
      
      Is there patch for the http V2.0 that will cause the
      compiled regular expressions to be freed?
      
      % parray tcl_platform
      tcl_platform(byteOrder) = littleEndian
      tcl_platform(machine)   = alpha
      tcl_platform(os)        = OSF1
      tcl_platform(osVersion) = V4.0
      tcl_platform(platform)  = unix
      tcl_platform(user)      = heller
      % set tcl_patchLevel
      8.2.0
      
      
      
    2. login: "rheller"
    3. mimetype: "text/plain"
    4. username: "rheller"