大佬教程收集整理的这篇文章主要介绍了Perl:当不需要超出范围的情况下,标量的内存被不必要地释放出来?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是我现在所做的:
$$extFileBufferRef = substr($$contentRef,$offset,$length,''); $length = length($$contentRef); my $content = substr($$contentRef,$length); $$contentRef = undef( $$contentRef) || $content;
$$contentRef可能是例如5 GB的大小在第一行,我提取4,9 GB的数据并替换提取的数据.第二行现在报告例如100 MB的数据作为字符串的长度,但例如Devel :: Size :: @R_850_10586@l_size仍然会输出为该标量分配5 GB的数据.并且将undef等等分配给$$contentRef似乎并没有改变这一点,所以我需要调用undef作为该标量的函数.
我可以预期,在使用substr后,$$contentRef背后的内存已经至少部分被释放了.似乎不是这样的…
那么,如果变量超出范围,内存是否被释放?如果是这样,为什么要将undef分配给不同于称为undef作为同一标量的函数?
$perl -MDevel::Peek -e' my $x; $x .= "x" for 1..100; Dump($X); substr($x,50,length($X),""); Dump($X); ' SV = PV(0x24208e0) at 0x243d550 ... CUR = 100 # length($X) == 100 LEN = 120 # 120 bytes are allocated for the String buffer. SV = PV(0x24208e0) at 0x243d550 ... CUR = 50 # length($X) == 50 LEN = 120 # 120 bytes are allocated for the String buffer.
Perl不仅会分配字符串,甚至不会释放超出范围的变量,而是在下次输入范围时重新使用它们.
$perl -MDevel::Peek -e' sub f { my ($set) = @_; my $x; if ($set) { $x = "abc"; $x .= "def"; } Dump($X); } f(1); f(0); ' SV = PV(0x3be74b0) at 0x3c04228 # PV: Scalar may contain a String REFCNT = 1 FLAGS = (POK,pPOK) # POK: Scalar contains a String PV = 0x3c0c6a0 "abcdef"\0 # The String buffer CUR = 6 LEN = 10 # Allocated size of the String buffer SV = PV(0x3be74b0) at 0x3c04228 # Could be a different scalar at the same address,REFCNT = 1 # but it's truly the same scalar FLAGS = () # No "OK" flags: undef PV = 0x3c0c6a0 "abcdef"\0 # The same String buffer CUR = 6 LEN = 10 # Allocated size of the String buffer
逻辑是,如果你需要一次内存,那么你很有可能再需要它了.
由于相同的原因,将undef分配给标量不会释放其字符串缓冲区.但Perl给你一个机会释放缓冲区,如果你想要的,所以传递一个标量到undef强制释放标量的内部缓冲区.
$perl -MDevel::Peek -e' my $x = "abc"; $x .= "def"; Dump($X); $x = undef; Dump($X); undef $x; Dump($X); ' SV = PV(0x37d1fb0) at 0x37eec98 # PV: Scalar may contain a String REFCNT = 1 FLAGS = (POK,pPOK) # POK: Scalar contains a String PV = 0x37e8290 "abcdef"\0 # The String buffer CUR = 6 LEN = 10 # Allocated size of the String buffer SV = PV(0x37d1fb0) at 0x37eec98 # PV: Scalar may contain a String REFCNT = 1 FLAGS = () # No "OK" flags: undef PV = 0x37e8290 "abcdef"\0 # The String buffer is still allcoated CUR = 6 LEN = 10 # Allocated size of the String buffer SV = PV(0x37d1fb0) at 0x37eec98 # PV: Scalar may contain a String REFCNT = 1 FLAGS = () # No "OK" flags: undef PV = 0 # The String buffer has been freed.
以上是大佬教程为你收集整理的Perl:当不需要超出范围的情况下,标量的内存被不必要地释放出来?全部内容,希望文章能够帮你解决Perl:当不需要超出范围的情况下,标量的内存被不必要地释放出来?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。