[Melbourne-pm] recovering memory used in IO::String or similar

Alfie John alfiejohn at gmail.com
Sat Sep 11 04:40:15 PDT 2010


Hey Malcolm,

If you're can have your contents in a file, you could get away with what you
want by using mmap:

  $ dd if=/dev/urandom count=1000000 of=/tmp/randfile

-- 8< --

#!/usr/bin/perl

use strict;
use warnings;

use IO::String;
use File::Map qw{
    map_file
    unmap
};

print_size();

map_file my $v, "/tmp/randfile";
print_size();

my $string = IO::String->new( $v );
print_size();

$string->getline();
print_size();

$string->close();
unmap( $v );
print_size();

sub print_size {
    open my $statm, '< /proc/self/statm'
        or die $!;

    my ( $size ) = split /\s/, <$statm>;
    $size *= 4096;

    print "size: $size\n";
}

-- >8 --

Output on a 512Mb file is:

  size: 21381120
  size: 533381120
  size: 533381120
  size: 533381120
  size: 21381120

If you can't put the contents in a file, you might still be able to get away
with using mmap but you'll have to play with Sys::Mmap (don't give new() the
OPTIONALFILENAME param).

Alfie

On Sat, Sep 11, 2010 at 8:03 AM, Malcolm Herbert <melbourne-pm at mjch.net>wrote:

> Sent again from the correct address ...
>
> On Sat, 11 Sep 2010 07:23 +1000, "Malcolm Herbert" <mjch at mjch.net>
> wrote:
> > I want to be able to use an IO::String file handle interface in order to
> > interact with one particular class, however I don't want all the data I
> > shove into the string to remain hanging around but be able to be retired
> > without closing and re-opening the handle - is there a way to do this?
> >
> > I have tried to resize the underlying tied variable with substr and
> > remove the aging data that way however this seems to remove the
> > association with the original tied variable ... plus leads to problems if
> > the class I'm using keeps internal state about the handle ...
> >
> > Regards,
> > Malcolm
>
> --
> Malcolm Herbert                                This brain intentionally
> mjch at mjch.net                                                left blank
>
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20100911/c683767a/attachment.html>


More information about the Melbourne-pm mailing list