代码之家  ›  专栏  ›  技术社区  ›  Anirudh Ramanathan

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

  •  2
  • Anirudh Ramanathan  · 技术社区  · 13 年前

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

    这是一个语言错误吗?关于做 $x=不设防 ,它不应该释放内存吗?

    1 回复  |  直到 13 年前
        1
  •  7
  •   ikegami Gilles Quénot    13 年前

    不。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 allocated