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

Malcolm Herbert melbourne-pm at mjch.net
Sat Sep 11 07:36:58 PDT 2010

Thanks to Alfie John and Mathew Robertson for their suggestions - 

On Sat, 11 Sep 2010 21:40 +1000, "Alfie John" <alfiejohn at gmail.com>
> If you're can have your contents in a file, you could get away with what
> you want by using mmap:

ooh ... nice, didn't even realise this was possible ... I like this a
lot ... :)

Unfortunately I'm dealing with a net stream, so I don't have it locally
on the filesystem in order to use this, but will definitely keep it in
mind for the future ... 

On Sat, 11 Sep 2010 19:23 +1000, "Mathew Robertson"
<mathew.blair.robertson at gmail.com> wrote:
> Have you tried 'truncate'  (see "perldoc IO::String")

I was looking to remove data from behind the file pointer actually,
rather than chop off the tail of the file, so truncate() wouldn't help
in this instance.  I grant you that I wasn't clear about that in the
original post ... :)

In the end I found that it was a combination of things that was causing
the corruption I was seeing in the code.  The IO::String problem turned
out to be reasonably simple to fix:

  my $buf = "";
  my $io = IO::String->new($buf);

then later:

  if (length($buf) > $MAXBUF) {
    my $pos = $io->pos;
    $buf = substr($buf, $MINBUF);
    $io->pos($pos - $MINBUF);
  $buf .= $data;

The main drawback from this is that tell(), pos() and seek() will
reference their position from the beginning of the string rather than
the bytes that have passed through the handle, but that doesn't seem to
bother the class that I'm interfacing with ...


Malcolm Herbert                                This brain intentionally
mjch at mjch.net                                                left blank

More information about the Melbourne-pm mailing list