Search code examples
linuxmemory-managementmmapmemory-fragmentation

Linux: how to check the largest contiguous address range available to a process


I want to enter the pid at the command line and get back the largest contiguous address space that has not been reserved. Any ideas?

Our 32 bit app, running on 64 bit RHEL 5.4, craps out after running for a while, say 24 hours. At that time it is only up to 2.5 gb of memory use, but we get out of memory errors. We think it failing to mmap large files because the app's memory space is fragmented. I wanted to go out to the production servers and just test that theory.


Solution

  • Slighly nicer version of my above comment:

    #!perl -T
    
    use warnings;
    use strict;
    
    scalar(@ARGV) > 0 or die "Use: $0 <pid>";
    
    my $pid = $ARGV[0];
    $pid = oct($pid) if $pid=~/^0/;         # support hex and octal PIDs
    $pid += 0; $pid = abs(int($pid));       # make sure we have a number
    
    open(my $maps, "<", "/proc/".$pid."/maps") or
            die "can't open maps file for pid ".$pid;
    
    my $max = 0;
    my $end = 0;
    while (<$maps>) {
            /([0-9a-f]+)-([0-9a-f]+)/;
            $max = hex ($1) - $end if $max < hex ($1) - $end;
            $end = hex ($2);
    }
    
    close ($maps);
    
    END {
            print "$max\n";
    }