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

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

问题描述

我在 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(), "
";

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

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