Tcl Source Code

Artifact [09a6703151]
Login

Artifact 09a670315101687e9fa8f4fa1e2eb3c42046257f:

Attachment "high_water_mark.tcl" to ticket [2960042fff] added by blacksqr 2010-03-01 12:09:25.
proc free_memory {} {
	set mem [exec vmstat]
	set mem [split $mem \n]
	set mem [lindex $mem 2]
	set mem [lindex $mem 3]
	set mem [expr $mem * 1000]
}

# Only significant aspect of this proc is that it drives the memory high water
# mark to almost the limit of available RAM.  Note string var is unset before 
# return.  This proc should have no side-effects, but interpreter doesn't
# release memory after return:
proc high_water_mark {} {
	set line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	for {set i 0} {$i < 10000000} {incr i} {
		append largeString $line\n
	}
	split $largeString \n
	unset largeString
	return
}

# Writes large file.
# Note string var is unset before return.
# This proc should have no side-effects:
proc large_file {fileName} {
	set line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	for {set i 0} {$i < 3000000} {incr i} {
		append largeString $line\n
	}
	set f [open $fileName w]
	puts $f $largeString
	close $f
	unset largeString
	return
}

# my computer reports about 2.9 gigabytes of free memory:
puts "free memory: [free_memory]"
exec echo "free memory: [free_memory]" | tee -a output.txt

# produces a file of about 246 MB.  Tcl handles it no problem:
large_file lf.txt

# about 2.6 GB left after previous proc:
puts "free memory: [free_memory]"
exec echo "free memory: [free_memory]" | tee -a output.txt

# this proc drives up RAM claimed by interpreter.
high_water_mark

# only about 100 MB of memory left:
puts "free memory: [free_memory]"
exec echo "free memory: [free_memory]" | tee -a output.txt

# create and write large file again:
large_file lf.txt

# interpreter crashes with memory allocation error before preceding proc completes.
# following lines are never output:

puts "free memory: [free_memory]"
exec echo "free memory: [free_memory]" | tee -a output.txt