TCL的线程缓存机制(永远)不会把通过tclsh主动释放的小块内存(例如unset)归还给system,一个tcl对象在free的时候优先进入自己的线程缓存池。
void
TclpFree(
void *ptr)
{
Cache *cachePtr;
Block *blockPtr;
int bucket;
if (ptr == NULL) {
return;
}
GETCACHE(cachePtr);
/*
* Get the block back from the user pointer and call system free directly
* for large blocks. Otherwise, push the block back on the bucket and move
* blocks to the shared cache if there are now too many free.
*/
blockPtr = Ptr2Block(ptr);
bucket = blockPtr->sourceBucket;
if (bucket == NBUCKETS) {
cachePtr->totalAssigned -= blockPtr->blockReqSize;
TclpSysFree(blockPtr);
return;
}
cachePtr->buckets[bucket].totalAssigned -= blockPtr->blockReqSize;
blockPtr->nextBlock = cachePtr->buckets[bucket].firstPtr;
cachePtr->buckets[bucket].firstPtr = blockPtr;
if (cachePtr->buckets[bucket].numFree == 0) {
cachePtr->buckets[bucket].lastPtr = blockPtr;
}
cachePtr->buckets[bucket].numFree++;
cachePtr->buckets[bucket].numInserts++;
if (cachePtr != sharedPtr &&
cachePtr->buckets[bucket].numFree > bucketInfo[bucket].maxBlocks) {
PutBlocks(cachePtr, bucket, bucketInfo[bucket].numMove);
}
}当然,除了中间特别大块会走TclpSysFree,其他正常情况下默认归还线程缓存。
如何定义这个大?tcl9给到了三个宏定义
#define MINALLOC ((sizeof(Block) + 8 + (TCL_ALLOCALIGN-1)) & ~(TCL_ALLOCALIGN-1))
#define NBUCKETS (11 - (MINALLOC >> 5))
#define MAXALLOC (MINALLOC << (NBUCKETS - 1))分别定义了最小块,桶数量和最大内存大小,超过就走系统。
对于重复分配再归还的情况TCL9与TCL8也有区别,这一点没有深究
#test script
proc rss_kb {} {
set f [open /proc/self/status r]
set data [read $f]
close $f
if {[regexp {VmRSS:\s+(\d+)\s+kB} $data -> v]} { return $v }
return -1
}
proc snap {label} {
puts [format "%-52s RSS = %8.2f MB" $label [expr {[rss_kb] / 1024.0}]]
}
proc Build {n} {
set l ""
for {set j 0} {$j < $n} {incr j} {
lappend l ${j}Xxxxxxxxxxxxxxxxxxxxxxxxxxqqqqqqqqqqqqqqqqqqqqqqq
}
return $l
}
snap "基线"
set ::big [Build 164000]
snap "全局 ::big 存活"
unset ::big
snap "unset ::big 之后"
#tcl8
% source test.tcl
基线 RSS = 2.50 MB
全局 ::big 存活 RSS = 31.25 MB
unset ::big 之后 RSS = 31.25 MB
#tcl9
% source 222.tcl
基线 RSS = 5.00 MB
全局 ::big 存活 RSS = 43.75 MB
unset ::big 之后 RSS = 43.75 MB盲猜是unicode的变化导致,感兴趣的朋友们可以深究
为什么tcl不归还给system? 根据官方的说法
garbage collection - A possible method to completely free memory in TCL? - Stack Overflow
if a process uses a lot of memory it's either going to exit soon, going to reuse that much memory
所以在分配的时候,不要一次性分配大量小块,单个小块用完之后立即free似乎比较合理,如果要创造一个巨大容器,那就要承担这部分内存对于系统而言永远常驻的问题。