0

I followed the top answer here, see below for code to get system memory being used at a given line in my perl code. I'm running Windows 7 Home Basic, 64 bit, 4 GB ram, 4.06 GB Virtual memory (As specified under advanced system settings under My computer). I got following message from perl at a line, before perl goes Out of Memory.

Memory usage: 1916346368

Assuming that this number is in bytes (= 1.78 GB), why did perl go Out of Memory? How can I get system total memory usage by all the processes?

Code block to compute memory is as below. I'm using Strawberry Perl 5.12.3.0

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";

Perl -V gives following info http://pastebin.com/mvF7YgKH

Update: The problem got solved with 64 bit perl. But I also noticed that the program ran w/o hitch on 32 bit perl on Ubuntu. So, may be Strawberry perl on Windows takes more memory than perl on Ubuntu.

Community
  • 1
  • 1
user13107
  • 3,239
  • 4
  • 34
  • 54

2 Answers2

3

If you were trying to allocate, or reallocate, an enormous string, array or hash it could have requested a larger memory block than was available from the system. While you have 4 gigs of real memory and 4 gigs of virtual, other processes might have been consuming a large chunk of that.

Alternatively, your system might have a per process memory limit which would might be 2 gigs.

Finally, your OS may be 64 bit, but your Perl may be 32 bit. In that case the maximum amount of memory it can address would be 2 gigs. If you post a copy of perl -V somewhere we can view we might be able to work that out.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Please see output of `perl -V` here http://pastebin.com/mvF7YgKH Updating the question too. I am not running any other heavy processes while perl is running. Browser, text editors etc. are all closed. – user13107 Nov 03 '12 at 20:18
  • @user13107, that looks like a 32 bit version of Perl. This may be the answer in that you may not have a 64 bit version installed, or you need to break up your problem into smaller bits. – titanofold Nov 03 '12 at 20:39
  • @titanofold yes, installing Strawberry perl 64 bit now (w/o uninstalling perl 32 bit). Will it create problem with already installed perl modules? Will I have to reinstall all the additional modules I installed from cpan? Thanks. – user13107 Nov 03 '12 at 20:42
  • @user13107 Yes, you'll have to reinstall your modules. They may have been compiled as 32 bit libraries or programs if they're not pure Perl programs. – titanofold Nov 03 '12 at 20:50
  • @titanofold Unfortunately, 64 bit perl didn't help. The program still exited at `Memory usage: 1916203008` (around the same as before). What might I be missing? I used a different perl executable that was present in `C:\strawberry64\perl\bin` – user13107 Nov 03 '12 at 20:56
  • @titanofold It even passed this test – user13107 Nov 03 '12 at 21:01
  • Something something something Windows joke. ;) – Schwern Nov 05 '12 at 05:18
0

You have 32-bit Perl. It is not surprising that Windows cannot allocate more than 2GB or memory to 32-bit process. If you install 64-bit Perl, this should max out at physical RAM.

mvp
  • 111,019
  • 13
  • 122
  • 148