首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$x = undef和undef $x之间的行为差异

$x = undef和undef $x之间的行为差异
EN

Stack Overflow用户
提问于 2013-03-13 03:29:31
回答 1查看 137关注 0票数 2

undef $x is different from $x = undef。我的印象是两者都会触发垃圾回收和释放内存,但$x = undef似乎没有这样做。

这是一个语言错误吗?在做$x = undef的时候,它不应该释放内存吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-13 03:31:51

不是也不是。与内存使用相比,Perl更看重速度,因为它不会释放您可能再次需要的内存。如果您希望释放字符串缓冲区,请使用undef $x;

代码语言:javascript
复制
$ 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 allocated
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15370435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档