Dear monks,
My first post here and I am afraid my question might be so unwise that I look silly but here goes. The members of this monastery have seemed during my many lurking years as being benevolent and kind so maybe I will not be ordered to serve too harsh a penance...
Update - the memory problem occurs even if I copy the arrays contrary to my this post, as described at the end.
Anyway... I'm using Chart clicker to make quite a few graphs (of network stats). Im having an out of memory exception half way through my list of graphs and as far as I thought it should not happen. I am passing in references to arrays to the function which creates the chart objects and I thought when the function returned all associated variables which were created inside the function would be freed but it seems not. If I dereference and copy the array the out of memory problem does not occur. Anyway its probably better for me to show some code which reproduces the problem, as I am not old and wise enough to be able to talk that much more about what I expect to happen and what exactly I am doing. Perhaps there is a simple obvious 'unrelated to chart clicker' action I am taking which I have gotten away with for all these years I have been using perl, or perhaps theres a less obvious reason, but the code will I am sure speak for itself far clearer than I ever can.
#!/usr/bin/perl -s -w
use Chart::Clicker;
use Chart::Clicker::Data::Series;
use Chart::Clicker::Data::DataSet;
$| = 1;
sub mysub {
($x, $y) = @_;
my $chart = Chart::Clicker->new;
my @x_copy = @$x;
my @y_copy = @$y;
my $series = Chart::Clicker::Data::Series->new(
keys => $x, # if I use \@x_copy and
values => $y, # \@y_copy here instead, there is no probl
+em
);
my $dataset = Chart::Clicker::Data::DataSet->new(
series => [ $series ],
);
$chart->add_to_datasets($dataset);
$chart->write_output('test.png');
}
my @x_axis;
my %a_hash;
for (my $t=0;$t<200;$t++){
$x_axis[$t] = $t*10;
@{$a_hash{abc}}[$t] = int(rand(100));
}
for (my $x=0;$x<10000;$x++) {
&mysub(\@x_axis, \@{$a_hash{abc}});
print ".";
}
As you can see in the comments if I use copied arrays there is no out of memory problem. If I return during the function anywhere before the $chart->write_output, there is no memory problem. But once $chart->write_output has been called, if I am using the original arrays (not local copies), I have an out of memory exception after few hundred graphs are created (in the above code it takes about 4000 graphs to trip the exception on a 2GB laptop, the original code is more complicated with multilined graphs etc and takes less iterations to die)
Is making a local copy of variables the only way to do this type of thing? Or am I messing up by passing the arrays in the way I am doing it? Or is chart::clicker at fault?
Many thanks in advance for your prayers and monastic guidance.
Update: It seems that although if I copy the arrays as described less memory is leaked, some is still leaked. If I copy the arrays as described the dataset I have (today) is able to complete (and not die) on the machine I am using but the process still grows with every call to the graph drawing sub. If I don't copy the arrays its not even able to complete. Since I will have to process larger datasets than the one I am testing with I don't even seem to have a workaround (apart from writing out temp files or some other kludge).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.