undef $x is different from $x = undef。我的印象是两者都会触发垃圾回收和释放内存,但$x = undef似乎没有这样做。
这是一个语言错误吗?在做$x = undef的时候,它不应该释放内存吗?
发布于 2013-03-13 03:31:51
不是也不是。与内存使用相比,Perl更看重速度,因为它不会释放您可能再次需要的内存。如果您希望释放字符串缓冲区,请使用undef $x;。
$ perl -MDevel::Peek -e'
Dump($x);
$x='abc'; Dump($x);
$x=undef; Dump($x);
undef $x; Dump($x);
'
SV = NULL(0x0) at 0x1c39284 <-- No body allocated
REFCNT = 1
FLAGS = () <-- Undefined
SV = PV(0x3e8d54) at 0x1c39284 <-- PV body allocated
REFCNT = 1
FLAGS = (POK,pPOK) <-- Contains a string
PV = 0x3eae7c "abc"\0
CUR = 3
LEN = 12
SV = PV(0x3e8d54) at 0x1c39284 <-- PV body allocated
REFCNT = 1
FLAGS = () <-- Undefined
PV = 0x3eae7c "abc"\0 <-- Currently unused string buffer
CUR = 3
LEN = 12
SV = PV(0x3e8d54) at 0x1c39284 <-- PV body allocated
REFCNT = 1
FLAGS = () <-- Undefined
PV = 0 <-- No string buffer allocatedhttps://stackoverflow.com/questions/15370435
复制相似问题