如何从内存中删除 perl 对象 [英] How can I remove perl object from memory

查看:37
本文介绍了如何从内存中删除 perl 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写的 perl 脚本(下面的代码)的内存使用方面遇到了一些问题.该脚本启动一些变量,用数据填充它们,然后再次取消定义它们.但是,删除所有内容后脚本的内存使用率仍然很高,不包含任何数据.

I'm having some issues with the memory usage of a perl script I wrote (code below). The script initiates some variables, fills them with data, and then undefines them again. However, the memory usage of the script after deleting everything is still way to high to contain no data.

根据 ps 脚本在前 39 秒内使用 1.027 Mb 内存 (RSS)(所以在 foreach 循环之前的所有内容).然后,内存使用量开始上升并最终在 204.391 Mb 和 172.410 Mb 之间波动.但是,即使在脚本的最后 10 秒(应该删除所有数据),内存使用量也不会低于 172.410 Mb.

Accoring to ps the script uses 1.027 Mb memory (RSS) during the first 39 seconds (so everything before the foreach loop). Then, memory usage starts rising and ends up fluctuating between 204.391 Mb and 172.410 Mb. However, even in the last 10 seconds of the script (where all data is supposed to be removed), memory usage never goes below 172.410 Mb.

有没有办法在perl中永久删除一个变量和其中的所有数据(为了减少脚本的内存使用)?如果是这样,我该怎么做?

Is there a way to permanently delete a variable and all data in it in perl (in order to reduce the memory usage of the script)? If so, how should I do it?

use strict;
use warnings;

sleep(30);

my $ELEMENTS = 1_000_000;
my $MAX_ELEMENT = 1_000_000_000;
my $if_condition = 1;

sleep(5);

my %hash = (1 => {}, 2 => {}, 3 => {}, 4 => {});

foreach my $key (keys %hash){
    if( $if_condition ){
        my $arrref1 = [ (rand($MAX_ELEMENT)) x $ELEMENTS ];
        my $arrref2 = [ (rand($MAX_ELEMENT)) x $ELEMENTS ];
        my $arrref3 = [ (rand($MAX_ELEMENT)) x $ELEMENTS ];

        sleep(2);

        if(!defined($hash{$key}->{'amplification'})){
            $hash{$key}->{'amplification'} = [];
        }

        push(@{$hash{$key}->{'amplification'}},@{$arrref1});
        undef($arrref1);
        push(@{$hash{$key}->{'amplification'}},@{$arrref2});
        undef($arrref2);
        push(@{$hash{$key}->{'amplification'}},@{$arrref3});
        undef($arrref3);


        sleep(3);

        delete($hash{$key});

        sleep(5);
    }
}

sleep(10);

推荐答案

Perl FAQ 3 - 如何释放数组或散列以便我的程序缩小?

Perl FAQ 3 - How can I free an array or hash so my program shrinks?

你通常不能.分配给词法的内存(即 my() 变量)即使超出范围也不能回收或重用.这是保留以防变量回到作用域.内存分配可以通过使用(在您的程序中)重用全局变量undef() 和/或 delete().

You usually can't. Memory allocated to lexicals (i.e. my() variables) cannot be reclaimed or reused even if they go out of scope. It is reserved in case the variables come back into scope. Memory allocated to global variables can be reused (within your program) by using undef() and/or delete().

在大多数操作系统上,内存分配程序永远无法返回到系统.这就是为什么长时间运行的程序有时会重新执行自己.一些经营系统(特别是使用 mmap(2) 分配大块的系统内存)可以回收不再使用的内存,但在这种情况下系统,必须配置和编译 perl 以使用操作系统的 malloc,不是 perl 的.

On most operating systems, memory allocated to a program can never be returned to the system. That's why long-running programs sometimes re- exec themselves. Some operating systems (notably, systems that use mmap(2) for allocating large chunks of memory) can reclaim memory that is no longer used, but on such systems, perl must be configured and compiled to use the OS's malloc, not perl's.

一般来说,内存分配和解除分配不是在 Perl 中你可以或应该担心的事情.

In general, memory allocation and de-allocation isn't something you can or should be worrying about much in Perl.

另见如何让我的 Perl 程序占用更少的内存?"

See also "How can I make my Perl program take less memory?"

这篇关于如何从内存中删除 perl 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆