Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

IPC::ShareLite documentation states that you can get a share's segment size:

my $size = $share->size;

but $share->size always returns 0.

Example:

use strict; use warnings; use IPC::ShareLite; use Storable qw( freeze thaw ); my $share; eval { $share = IPC::ShareLite->new( -key => 4812, -create => 'yes', -destroy => 'no', -mode => 0600, ); }; if ($@) { warn "Woops: $@"; exit; } # Let's use some memory my @array = 0 .. 1500000; $share->store( freeze( \@array ) ); my $size = $share->size; print "Size: $size\n"; my $num_segments = $share->num_segments; print "Segments: $num_segments\n";

which yields:

Size: 0
Segments: 207

Any hints are very appreciated.

Replies are listed 'Best First'.
Re: IPC::ShareLite - getting a shares size
by zentara (Cardinal) on May 17, 2012 at 20:16 UTC
    I'm not sure you are interpreting size correctly. It is not the total size of all the memory used, but the segment size you desire. You need to set the segment size in the constructor. You were leaving it as zero by default.
    #!/usr/bin/perl use strict; use warnings; use IPC::ShareLite; use Storable qw( freeze thaw ); # dosn't show segment size correctly without size set. my $share; eval { $share = IPC::ShareLite->new( -key => 4812, -create => 'yes', -destroy => 'no', -mode => 0600, -size => 5000 ); }; if ($@) { warn "Woops: $@"; exit; } # Let's use some memory my @array = (0 .. 1500000); $share->store( freeze( \@array ) ); my $size = $share->size; print "Size: $size\n"; my $num_segments = $share->num_segments; print "Segments: $num_segments\n"; my $usage = $share->size * $share->num_segments; print "Usage: $usage\n";

    OUTPUT:

    $ ./971163.pl Size: 5000 Segments: 207 Usage: 1035000

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thank you. You made my day.