From: Robert Klemme Date: 2009-03-19T01:25:44+09:00 Subject: Re: free RAM problem --0003255762cafd5dbf0465672bb5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 2009/3/17 Eleanor McHugh : > On 17 Mar 2009, at 15:23, Edouard Dantes wrote: >> well, the loops are too basic to create problems, it's fill array with >> values & write to file. >> >> RAM jumps are too erratic (5 or 10% for 2GB RAM) >> >> The only processor angry stuff (and possibly RAM too) is the following >> parsing line, imported from another file. >> >> Nokogiri::HTML(page).css('div a').map{|dts| >> dts.text.gsub(/\s+/,"").gsub(/[^A-Za-z0-9\s]/,'Z') } >> >> This line runs thousands of time, might the implied regex feed RAM with >> datas without cleaning? > > I'm not sure it'll make much difference but moving the regex creation int= o > the enclosing scope seems neater: > > =A0 =A0 =A0 =A0spaces =3D /\s+/ > =A0 =A0 =A0 =A0chars =3D /[^A-Za-z0-9\s]/ This is generally less efficient and AFAIK there are no memory leaks attached to this. It's a different story with varying regular expressions (i.e. based on input or configuration). But in this case I'd leave them in place. > =A0 =A0 =A0 =A0Nokogiri::HTML(page).css('div a').map { |dts| > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0dts.text.gsub(spaces, "").gsub(chars, 'Z') > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > > Likewise changing the logic to use gsub! will reduce the number of transi= ent > string objects being created and that will reduce memory usage. True, maybe the first gsub needs to stay in order to avoid aliasing effects= . > I'd also be interested to see the code you're using for writing to file a= s > the nature of the RAM usage jumps suggests you could be experiencing a > buffering artefact. There was also an issue in 1.8.* related to Array#shift or #unshift. And I agree with your general statement to not explicitly force GC. Rather look for memory leaks. Edouard, you might get a first idea where you spend your memory by dumping instance counts in intervals (sample attached). Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end --0003255762cafd5dbf0465672bb5 Content-Type: application/octet-stream; name="mem-stat.rb" Content-Disposition: attachment; filename="mem-stat.rb" Content-Transfer-Encoding: base64 X-Attachment-Id: f_fsg898cm0 I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