2

I have some Perl code:

use HTML::Parse;
use HTML::FormatText;

# ...

my $txtFormatter = HTML::FormatText->new();

while ( ... ) {   # some condition
    my $txt = # get from a file
    my $html_tree = HTML::TreeBuilder->new_from_content($txt);
    $txt = $txtFormatter->format($html_tree);
    $html_tree->delete();
    # write $txt to a file
}

I noticed the perl.exe process steadily increases in size (up to 600 MB after 2 million or so loop iterations). If I take out the HTML::TreeBuilder stuff, it does not increase at all. Is there anything I can do to plug this leak?

cjm
  • 61,471
  • 9
  • 126
  • 175
JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • See: [Common Perl memory reference leak patterns](http://stackoverflow.com/questions/2223721/common-perl-memory-reference-leak-patterns) and [Perl memory usage: profiling and leak detection](http://stackoverflow.com/questions/1359771/perl-memory-usage-profiling-and-leak-detection). – Ether Aug 05 '10 at 17:28
  • That's fine, but I don't want to debug HTML::TreeBuilder! – JoelFan Aug 05 '10 at 17:53
  • 1
    Which version of `HTML::TreeBuilder` are you using? Which version of `perl`? You get the picture. – Sinan Ünür Aug 05 '10 at 21:41
  • 1
    It seems to me the cardinal rule of programming applies here: Always assume the problem is in your code, before blaming the library, the compiler, the universe and everything. Voting to close as *too localized* as you have not provided a small, self-contained example that exhibits the problem. – Sinan Ünür Aug 05 '10 at 21:57

1 Answers1

0

I cannot replicate this with the following script:

#!/usr/bin/perl

use strict; use warnings;

use File::Slurp;
use HTML::FormatText;
use HTML::TreeBuilder;

my $formatter = HTML::FormatText->new;
my $html = read_file 'test.html';

while ( 1 ) {
    my $tree = HTML::TreeBuilder->new_from_content( $html );
    $formatter->format( $tree );
    $tree->delete;
}

I let this script run for minutes and the memory usage (in Task Manager) remained between 7,200K and 7,300K.

E:\Home> perl -v

This is perl, v5.10.1 built for MSWin32-x86-multi-thread
(with 2 registered patches, see perl -V for more detail)

Copyright 1987-2009, Larry Wall

Binary build 1006 [291086] provided by ActiveState http://www.ActiveState.com
Built Aug 24 2009 13:48:26
E:\Home> perl -MHTML::TreeBuilder -e "print $HTML::TreeBuilder::VERSION"
3.23
E:\Home> perl -MHTML::FormatText -e "print $HTML::FormatText::VERSION"
2.04
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • I have the same versions of TreeBuilder and FormatText. My Perl is 5.12.0, however, and it's Strawberry, not ActiveState. Hmmm... – JoelFan Aug 06 '10 at 14:28
  • I don't have Strawberry's `perl` 5.12.0 on this system. However, with Strawberry `perl` 5.10.1, I see memory use stay between 7,600K and 7,700K. – Sinan Ünür Aug 06 '10 at 17:42