如何在Windows下以编程方式确定Perl程序的内存使用情况? [英] How can I programmatically determine my Perl program's memory usage under Windows?

查看:52
本文介绍了如何在Windows下以编程方式确定Perl程序的内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Windows下的ActivePerl用于我的Perl脚本,因此我可以通过Windows任务管理器中的进程"选项卡查看它使用了多少内存.

I use ActivePerl under Windows for my Perl script, so I can look at how much memory it uses via the 'Processes' tab in Windows Task Manager.

我发现必须这样做很麻烦.有没有其他方法可以确定我的Perl程序的内存使用情况?

I find having to do this rather cumbersome. Is there another way to determine my Perl program's memory use?

推荐答案

如果您使用的是ActivePerl,则其中某些解决方案将无法使用.我已经整理了一些我认为应该在ActivePerl中可以立即使用的东西,但是还没有在5.10以下的版本中对其进行过测试,因此您的行驶里程可能会有所不同.正如Pax回答的那样,您可以根据自己的要求获得不同的数字,例如MaximumWorkingSetSizeWorkingSetSize等.

If you're using ActivePerl, some of these solutions won't work. I've cobbled together something I think should work out of the box in ActivePerl, but it hasn't been tested in less than 5.10, so your mileage may vary. As Pax answered, you can get different numbers depending on what you ask for, i.e., MaximumWorkingSetSize vs WorkingSetSize, etc.

use Win32::OLE qw/in/;

sub memory_usage() {
    my $objWMI = Win32::OLE->GetObject('winmgmts:\\\\.\\root\\cimv2');
    my $processes = $objWMI->ExecQuery("select * from Win32_Process where ProcessId=$$");

    foreach my $proc (in($processes)) {
        return $proc->{WorkingSetSize};
    }
}

print 'Memory usage: ', memory_usage(), "\n";

这篇关于如何在Windows下以编程方式确定Perl程序的内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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