Tcl Source Code

Hex Artifact Content
Login

Artifact 09a670315101687e9fa8f4fa1e2eb3c42046257f:

Attachment "high_water_mark.tcl" to ticket [2960042fff] added by blacksqr 2010-03-01 12:09:25.
0000: 70 72 6f 63 20 66 72 65 65 5f 6d 65 6d 6f 72 79  proc free_memory
0010: 20 7b 7d 20 7b 0a 09 73 65 74 20 6d 65 6d 20 5b   {} {..set mem [
0020: 65 78 65 63 20 76 6d 73 74 61 74 5d 0a 09 73 65  exec vmstat]..se
0030: 74 20 6d 65 6d 20 5b 73 70 6c 69 74 20 24 6d 65  t mem [split $me
0040: 6d 20 5c 6e 5d 0a 09 73 65 74 20 6d 65 6d 20 5b  m \n]..set mem [
0050: 6c 69 6e 64 65 78 20 24 6d 65 6d 20 32 5d 0a 09  lindex $mem 2]..
0060: 73 65 74 20 6d 65 6d 20 5b 6c 69 6e 64 65 78 20  set mem [lindex 
0070: 24 6d 65 6d 20 33 5d 0a 09 73 65 74 20 6d 65 6d  $mem 3]..set mem
0080: 20 5b 65 78 70 72 20 24 6d 65 6d 20 2a 20 31 30   [expr $mem * 10
0090: 30 30 5d 0a 7d 0a 0a 23 20 4f 6e 6c 79 20 73 69  00].}..# Only si
00a0: 67 6e 69 66 69 63 61 6e 74 20 61 73 70 65 63 74  gnificant aspect
00b0: 20 6f 66 20 74 68 69 73 20 70 72 6f 63 20 69 73   of this proc is
00c0: 20 74 68 61 74 20 69 74 20 64 72 69 76 65 73 20   that it drives 
00d0: 74 68 65 20 6d 65 6d 6f 72 79 20 68 69 67 68 20  the memory high 
00e0: 77 61 74 65 72 0a 23 20 6d 61 72 6b 20 74 6f 20  water.# mark to 
00f0: 61 6c 6d 6f 73 74 20 74 68 65 20 6c 69 6d 69 74  almost the limit
0100: 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 52 41   of available RA
0110: 4d 2e 20 20 4e 6f 74 65 20 73 74 72 69 6e 67 20  M.  Note string 
0120: 76 61 72 20 69 73 20 75 6e 73 65 74 20 62 65 66  var is unset bef
0130: 6f 72 65 20 0a 23 20 72 65 74 75 72 6e 2e 20 20  ore .# return.  
0140: 54 68 69 73 20 70 72 6f 63 20 73 68 6f 75 6c 64  This proc should
0150: 20 68 61 76 65 20 6e 6f 20 73 69 64 65 2d 65 66   have no side-ef
0160: 66 65 63 74 73 2c 20 62 75 74 20 69 6e 74 65 72  fects, but inter
0170: 70 72 65 74 65 72 20 64 6f 65 73 6e 27 74 0a 23  preter doesn't.#
0180: 20 72 65 6c 65 61 73 65 20 6d 65 6d 6f 72 79 20   release memory 
0190: 61 66 74 65 72 20 72 65 74 75 72 6e 3a 0a 70 72  after return:.pr
01a0: 6f 63 20 68 69 67 68 5f 77 61 74 65 72 5f 6d 61  oc high_water_ma
01b0: 72 6b 20 7b 7d 20 7b 0a 09 73 65 74 20 6c 69 6e  rk {} {..set lin
01c0: 65 20 78 78 78 78 78 78 78 78 78 78 78 78 78 78  e xxxxxxxxxxxxxx
01d0: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
01e0: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
01f0: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
0200: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
0210: 78 78 78 78 78 78 78 0a 09 66 6f 72 20 7b 73 65  xxxxxxx..for {se
0220: 74 20 69 20 30 7d 20 7b 24 69 20 3c 20 31 30 30  t i 0} {$i < 100
0230: 30 30 30 30 30 7d 20 7b 69 6e 63 72 20 69 7d 20  00000} {incr i} 
0240: 7b 0a 09 09 61 70 70 65 6e 64 20 6c 61 72 67 65  {...append large
0250: 53 74 72 69 6e 67 20 24 6c 69 6e 65 5c 6e 0a 09  String $line\n..
0260: 7d 0a 09 73 70 6c 69 74 20 24 6c 61 72 67 65 53  }..split $largeS
0270: 74 72 69 6e 67 20 5c 6e 0a 09 75 6e 73 65 74 20  tring \n..unset 
0280: 6c 61 72 67 65 53 74 72 69 6e 67 0a 09 72 65 74  largeString..ret
0290: 75 72 6e 0a 7d 0a 0a 23 20 57 72 69 74 65 73 20  urn.}..# Writes 
02a0: 6c 61 72 67 65 20 66 69 6c 65 2e 0a 23 20 4e 6f  large file..# No
02b0: 74 65 20 73 74 72 69 6e 67 20 76 61 72 20 69 73  te string var is
02c0: 20 75 6e 73 65 74 20 62 65 66 6f 72 65 20 72 65   unset before re
02d0: 74 75 72 6e 2e 0a 23 20 54 68 69 73 20 70 72 6f  turn..# This pro
02e0: 63 20 73 68 6f 75 6c 64 20 68 61 76 65 20 6e 6f  c should have no
02f0: 20 73 69 64 65 2d 65 66 66 65 63 74 73 3a 0a 70   side-effects:.p
0300: 72 6f 63 20 6c 61 72 67 65 5f 66 69 6c 65 20 7b  roc large_file {
0310: 66 69 6c 65 4e 61 6d 65 7d 20 7b 0a 09 73 65 74  fileName} {..set
0320: 20 6c 69 6e 65 20 78 78 78 78 78 78 78 78 78 78   line xxxxxxxxxx
0330: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
0340: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
0350: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
0360: 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78 78  xxxxxxxxxxxxxxxx
0370: 78 78 78 78 78 78 78 78 78 78 78 0a 09 66 6f 72  xxxxxxxxxxx..for
0380: 20 7b 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c   {set i 0} {$i <
0390: 20 33 30 30 30 30 30 30 7d 20 7b 69 6e 63 72 20   3000000} {incr 
03a0: 69 7d 20 7b 0a 09 09 61 70 70 65 6e 64 20 6c 61  i} {...append la
03b0: 72 67 65 53 74 72 69 6e 67 20 24 6c 69 6e 65 5c  rgeString $line\
03c0: 6e 0a 09 7d 0a 09 73 65 74 20 66 20 5b 6f 70 65  n..}..set f [ope
03d0: 6e 20 24 66 69 6c 65 4e 61 6d 65 20 77 5d 0a 09  n $fileName w]..
03e0: 70 75 74 73 20 24 66 20 24 6c 61 72 67 65 53 74  puts $f $largeSt
03f0: 72 69 6e 67 0a 09 63 6c 6f 73 65 20 24 66 0a 09  ring..close $f..
0400: 75 6e 73 65 74 20 6c 61 72 67 65 53 74 72 69 6e  unset largeStrin
0410: 67 0a 09 72 65 74 75 72 6e 0a 7d 0a 0a 23 20 6d  g..return.}..# m
0420: 79 20 63 6f 6d 70 75 74 65 72 20 72 65 70 6f 72  y computer repor
0430: 74 73 20 61 62 6f 75 74 20 32 2e 39 20 67 69 67  ts about 2.9 gig
0440: 61 62 79 74 65 73 20 6f 66 20 66 72 65 65 20 6d  abytes of free m
0450: 65 6d 6f 72 79 3a 0a 70 75 74 73 20 22 66 72 65  emory:.puts "fre
0460: 65 20 6d 65 6d 6f 72 79 3a 20 5b 66 72 65 65 5f  e memory: [free_
0470: 6d 65 6d 6f 72 79 5d 22 0a 65 78 65 63 20 65 63  memory]".exec ec
0480: 68 6f 20 22 66 72 65 65 20 6d 65 6d 6f 72 79 3a  ho "free memory:
0490: 20 5b 66 72 65 65 5f 6d 65 6d 6f 72 79 5d 22 20   [free_memory]" 
04a0: 7c 20 74 65 65 20 2d 61 20 6f 75 74 70 75 74 2e  | tee -a output.
04b0: 74 78 74 0a 0a 23 20 70 72 6f 64 75 63 65 73 20  txt..# produces 
04c0: 61 20 66 69 6c 65 20 6f 66 20 61 62 6f 75 74 20  a file of about 
04d0: 32 34 36 20 4d 42 2e 20 20 54 63 6c 20 68 61 6e  246 MB.  Tcl han
04e0: 64 6c 65 73 20 69 74 20 6e 6f 20 70 72 6f 62 6c  dles it no probl
04f0: 65 6d 3a 0a 6c 61 72 67 65 5f 66 69 6c 65 20 6c  em:.large_file l
0500: 66 2e 74 78 74 0a 0a 23 20 61 62 6f 75 74 20 32  f.txt..# about 2
0510: 2e 36 20 47 42 20 6c 65 66 74 20 61 66 74 65 72  .6 GB left after
0520: 20 70 72 65 76 69 6f 75 73 20 70 72 6f 63 3a 0a   previous proc:.
0530: 70 75 74 73 20 22 66 72 65 65 20 6d 65 6d 6f 72  puts "free memor
0540: 79 3a 20 5b 66 72 65 65 5f 6d 65 6d 6f 72 79 5d  y: [free_memory]
0550: 22 0a 65 78 65 63 20 65 63 68 6f 20 22 66 72 65  ".exec echo "fre
0560: 65 20 6d 65 6d 6f 72 79 3a 20 5b 66 72 65 65 5f  e memory: [free_
0570: 6d 65 6d 6f 72 79 5d 22 20 7c 20 74 65 65 20 2d  memory]" | tee -
0580: 61 20 6f 75 74 70 75 74 2e 74 78 74 0a 0a 23 20  a output.txt..# 
0590: 74 68 69 73 20 70 72 6f 63 20 64 72 69 76 65 73  this proc drives
05a0: 20 75 70 20 52 41 4d 20 63 6c 61 69 6d 65 64 20   up RAM claimed 
05b0: 62 79 20 69 6e 74 65 72 70 72 65 74 65 72 2e 0a  by interpreter..
05c0: 68 69 67 68 5f 77 61 74 65 72 5f 6d 61 72 6b 0a  high_water_mark.
05d0: 0a 23 20 6f 6e 6c 79 20 61 62 6f 75 74 20 31 30  .# only about 10
05e0: 30 20 4d 42 20 6f 66 20 6d 65 6d 6f 72 79 20 6c  0 MB of memory l
05f0: 65 66 74 3a 0a 70 75 74 73 20 22 66 72 65 65 20  eft:.puts "free 
0600: 6d 65 6d 6f 72 79 3a 20 5b 66 72 65 65 5f 6d 65  memory: [free_me
0610: 6d 6f 72 79 5d 22 0a 65 78 65 63 20 65 63 68 6f  mory]".exec echo
0620: 20 22 66 72 65 65 20 6d 65 6d 6f 72 79 3a 20 5b   "free memory: [
0630: 66 72 65 65 5f 6d 65 6d 6f 72 79 5d 22 20 7c 20  free_memory]" | 
0640: 74 65 65 20 2d 61 20 6f 75 74 70 75 74 2e 74 78  tee -a output.tx
0650: 74 0a 0a 23 20 63 72 65 61 74 65 20 61 6e 64 20  t..# create and 
0660: 77 72 69 74 65 20 6c 61 72 67 65 20 66 69 6c 65  write large file
0670: 20 61 67 61 69 6e 3a 0a 6c 61 72 67 65 5f 66 69   again:.large_fi
0680: 6c 65 20 6c 66 2e 74 78 74 0a 0a 23 20 69 6e 74  le lf.txt..# int
0690: 65 72 70 72 65 74 65 72 20 63 72 61 73 68 65 73  erpreter crashes
06a0: 20 77 69 74 68 20 6d 65 6d 6f 72 79 20 61 6c 6c   with memory all
06b0: 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 62 65  ocation error be
06c0: 66 6f 72 65 20 70 72 65 63 65 64 69 6e 67 20 70  fore preceding p
06d0: 72 6f 63 20 63 6f 6d 70 6c 65 74 65 73 2e 0a 23  roc completes..#
06e0: 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 69 6e 65 73   following lines
06f0: 20 61 72 65 20 6e 65 76 65 72 20 6f 75 74 70 75   are never outpu
0700: 74 3a 0a 0a 70 75 74 73 20 22 66 72 65 65 20 6d  t:..puts "free m
0710: 65 6d 6f 72 79 3a 20 5b 66 72 65 65 5f 6d 65 6d  emory: [free_mem
0720: 6f 72 79 5d 22 0a 65 78 65 63 20 65 63 68 6f 20  ory]".exec echo 
0730: 22 66 72 65 65 20 6d 65 6d 6f 72 79 3a 20 5b 66  "free memory: [f
0740: 72 65 65 5f 6d 65 6d 6f 72 79 5d 22 20 7c 20 74  ree_memory]" | t
0750: 65 65 20 2d 61 20 6f 75 74 70 75 74 2e 74 78 74  ee -a output.txt
0760: 0a                                               .