Perl Catch Out Of Memory Error
Poetry RecentThreads NewestNodes Donate What'sNew on Mar 25, 2003 at 20:18UTC ( #245771=perlquestion: print w/replies, xml ) Need Help?? artist has asked for the wisdom of the Perl Monks concerning the out of memory error while running perl script following question: Dear Monks, For a large calculation, I need to perl out of memory reading large file have some adjustement according to the memory available. How I can catch the out of memory errors at run time ?. Thanks, artist.Comment on Catching "out of memory " error Replies are listed 'Best First'. Re: Catching "out of memory " error by perrin (Chancellor) on Mar 25, 2003 at 21:11UTC You can't catch an "out of memory" error. It means what it says: there's no more memory to use! One thing you could try is measuring the amount of memory your process uses as you go and aborting before you run out of memory.[reply] Re: Catching "out of memory " error by OM_Zen (Scribe) on Mar 25, 2003 at 21:53UTC Hi, This might be a lateral idea or something and not a solution to the requirement . The setting of the $^M pre-defined variable which can be a setting for an emergency memory pooling of which more details are in perldoc perlvar for details on this variable [reply][d/l] Re: Catching "out of memory " error by bluto (Curate) on Mar 25, 2003 at 21:54UTC As stated by others, you can't do this with any consitency. You may however be able to estimate how much memory a given calculation will use by running smaller calculations and watching the memory usage (e.g. with 'ps'). You'll probably want to limit your memory usage to some percentage of total real memory (rather than virtual memory) for best performance, but YMMV. bluto[reply] Re: Catching "out of memory " error by Zaxo (Archbishop) on Mar 25, 2003 at 20:58UTC The usual method for throw and catch of exceptions is to eval, then test $@. use Errno; eval { # ..
put in the rest of this book is the core of Perl, the part that every Perl user should understand. But there are a few other techniques that, while not obligatory, are still valuable tools to have in your toolbox. We've gathered the most important of those for this chapter. Don't be misled by the title of the chapter, though; the techniques here aren't especially more difficult to understand than what we have elsewhere. They are "advanced" merely in the sense that they aren't necessary for beginners. The first time you read this book, you may want to skip (or skim) this chapter so you can get right to using Perl. Come back to it a http://www.perlmonks.org/?node_id=245771 month or two later, when you're ready to get even more out of Perl. Consider this entire chapter a huge footnote[361]. [361]We contemplated doing that in one of the drafts, but got firmly rejected by O'Reilly's editors. 17.1. Trapping Errors with eval Sometimes, your ordinary, everyday code can cause a fatal error in your program. Each of these typical statements could crash a program: $barney = $fred / $dino; # divide-by-zero error? print "match\n" http://docstore.mik.ua/orelly/perl4/lperl/ch17_01.htm if /^($wilma)/; # illegal regular expression error? open CAVEMAN, $fred # user-generated error from die? or die "Can't open file '$fred' for input: $!"; You could go to some trouble to catch some of these, but it's hard to get them all. (How could you check the string $wilma from that example to ensure that it makes a valid regular expression?) Fortunately, Perl provides a simple way to catch fatal errors: wrap the code in an eval block: eval { $barney = $fred / $dino } ; Now, even if $dino is zero, that line won't crash the program. The eval is actually an expression (not a control structure, like while or foreach) so that semicolon is required at the end of the block. When a normally fatal error happens during the execution of an eval block, the block is done running, but the program doesn't crash. So that means that right after an eval finishes, you'll be wanting to know whether it exited normally or whether it caught a fatal error for you. The answer is in the special $@ variable. If the eval caught a fatal error, $@ will hold what would have been the program's dying words, perhaps something like: Illegal division by zero at my_program line 12. If there was no error, $@ will be empty. Of course, that means th
PERL Beginners I wrote a small script that uses message ID's as unique values and extracts recipient address info. The goal is to count 1019 events per message ID. It also gets the sum of recipients per message ID. The script http://www.justskins.com/forums/out-of-memory-error-115248.html works fine but when it runs against a very large file (2GB+) I receive an out of memory error. Is there a more efficient way of handling the hash portion that is less memory intense and preferably faster? --Paul # Tracking log pr use strict; my $recips; my %event_id; my $counter; my $total_recips; my $count; # Get log file die "You ... Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode out of Switch to Hybrid Mode Switch to Threaded Mode December 16th,07:15 PM #1 Out of memory error problem I wrote a small script that uses message ID's as unique values and extracts recipient address info. The goal is to count 1019 events per message ID. It also gets the sum of recipients per message ID. The script works fine but when it runs against a very large file (2GB+) I receive an out of memory error. Is there out of memory a more efficient way of handling the hash portion that is less memory intense and preferably faster? --Paul # Tracking log pr use strict; my $recips; my %event_id; my $counter; my $total_recips; my $count; # Get log file die "You must enter a tracking log. \n" if $#ARGV <0; my $logfile = shift; open (LOGFILE, $logfile) || die "Unable to open $logfile because\n $!\n"; foreach (