spica1001 has asked for the wisdom of the Perl Monks concerning the following question:
Hi all. Here's an odd one, to me anyway. I'm running a script to process very large XML files with embedded JSON. When I run it, the memory increases indefinitely (the file can be 100s of GB and the RAM usage reaches 25GB+). Boiling it down, I reach the below. If I remove the line marked "## THIS LINE", the memory remains static, but leave it in and it increases again. Adding in a load of undefs seems to make no difference. It's evidently leaving the hash array around, but I can't see how to 'free' it.
Why would accessing a non-existent hash value cause that, or of course even better how do I prevent it?! Thanks for any help...
use JSON; open(IN,"<:utf8","$ARGV[0]"); while(<IN>) { if (m!^\s+<text.*?>({[^\{\|].+})</text>!) { my $jt = $+; $jt=~s/\"/\"/g; my $json = new JSON; my $jp = $json->allow_nonref->utf8->relaxed->decode($jt); my $c = $jp->{'claims'}; # "claims":{"P31":[{"mainsnak":{"snaktype":"value","property": +"P31","hash":"...","datavalue":{"value":{"entity-type":"item","numeri +c-id":5},...}... }...}], if (ref($c) eq 'HASH') { foreach my $ch (keys %$c) { if (ref($c->{$ch}) eq 'ARRAY') { foreach my $cg (@{$c->{$ch}}) { if (defined $cg->{'mainsnak'}->{'datavalue'}-> +{'value'}->{'notexist'}) {} ## THIS LINE } } } } } }
====================================================
UPDATE: Thanks to all the replies, I've worked it out now.
Points should go to tinita as the suggestion of use strict pointed me in the right direction. It turned out that, in some of the lines that my script reads, the value $cg->{'mainsnak'}->{'datavalue'}->{'value'} is a string, not a hash. It seems treating a string as a hash causes the memory growth. I fixed it with:
if (ref($cg->{'mainsnak'}->{'datavalue'}->{'value'}) eq 'HASH' && defined $cg->{'mainsnak'}->{'datavalue'}->{'value'}->{'notexist'}) {}(Of course my code does plenty else besides this, but the principle of needing to check that a variable is indeed a hash before checking for a key is the main takeaway here.)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Strange memory growth
by ikegami (Patriarch) on Feb 14, 2018 at 18:26 UTC | |
by tinita (Parson) on Feb 14, 2018 at 18:50 UTC | |
by ikegami (Patriarch) on Feb 14, 2018 at 18:54 UTC | |
by tinita (Parson) on Feb 14, 2018 at 18:56 UTC | |
Re: Strange memory growth
by huck (Prior) on Feb 14, 2018 at 18:28 UTC | |
by spica1001 (Initiate) on Feb 19, 2018 at 14:58 UTC | |
Re: Strange memory growth
by tinita (Parson) on Feb 14, 2018 at 18:42 UTC | |
by Anonymous Monk on Feb 16, 2018 at 14:50 UTC | |
Re: Strange memory growth
by Marshall (Canon) on Feb 15, 2018 at 06:36 UTC | |
by Anonymous Monk on Feb 15, 2018 at 13:29 UTC | |
by Anonymous Monk on Feb 15, 2018 at 14:13 UTC | |
by Anonymous Monk on Feb 16, 2018 at 14:46 UTC | |
by Marshall (Canon) on Feb 16, 2018 at 21:48 UTC | |
Re: Strange memory growth (Data::Diver)
by Anonymous Monk on Feb 17, 2018 at 20:46 UTC |