Skip to content

Instantly share code, notes, and snippets.

@cosimo
Last active June 18, 2019 11:27
Show Gist options
  • Save cosimo/e90a9f6daa1fbdc62a89843cf27f005a to your computer and use it in GitHub Desktop.
Save cosimo/e90a9f6daa1fbdc62a89843cf27f005a to your computer and use it in GitHub Desktop.
Shows process memory segment sizes
#!/usr/bin/env perl
=head1 NAME
map-size.pl - Shows process memory segment sizes
=head1 SYNOPSIS
cat /proc/[pid]/maps | ./map-size.pl
=head1 DESCRIPTION
Takes C</proc/[pid]/maps> file as input (process memory map or list of segments
of memory used up by a given process) and for each one, calculates its size
in megabytes.
This is useful for example when trying to check how big the stack of each jvm
thread is using. Probably like using a stone tool to cut sashimi, but that
is the level of my knowledge of the jvm so far, and I'm not able to use
visualvm against a remote host.
=cut
use 5.014;
use strict;
use open qw( :encoding(UTF-8) :std );
use utf8;
use warnings qw( FATAL utf8 );
use autodie;
use Getopt::Long qw( GetOptions );
sub to_mb($) {
my $num = shift;
return sprintf("%.2fM", $num / 1024 / 1024);
}
GetOptions(
'v' => \(my $verbose = 0),
);
my $total_mem = 0;
my $threads = 0;
my %tag_sum;
while (readline) {
chomp;
my ($mem, $perms, $flags, $pages, $num, $tag) = split m{\s+};
my ($from, $to) = split "-", $mem;
my $size = hex($to) - hex($from);
if ($tag =~ m{^ \[ stack : .+ \] $}x) {
$tag_sum{"[stack]"} += $size;
$threads++;
}
$tag_sum{$tag} += $size;
$total_mem += $size;
my $size_mb = to_mb($size);
my $rest = join("\t", $perms, $flags, $pages, $num, $tag);
say join("\t\t", $size . " ($size_mb)", $mem, $rest) if $verbose;
}
my $total_mem_mb = to_mb($total_mem);
say "—" x 80 if $verbose;
say "Threads: $threads";
say "Total: $total_mem_mb ($total_mem)";
say "Stack: ", to_mb($tag_sum{"[stack]"});
if ($verbose) {
for (sort keys %tag_sum) {
my $tag = $_ || '<dynamic>';
say " — ", $tag, " = ", to_mb($tag_sum{$_});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment