简体   繁体   English

Ruby gsub,NoMemoryError:无法分配内存。

[英]Ruby gsub, NoMemoryError: failed to allocate memory.

We have a html template cached - which has placeholder values. 我们缓存了一个html模板-具有占位符值。 Based on the real object values, the placeholders are being replaced. 根据实际对象的值,将替换占位符。 This works fine mostly but sometimes results in error(failed to allocate memory). 这通常可以正常工作,但有时会导致错误(分配内存失败)。

    replacement_hash.keys.each do |placeholder|
      value = ##some logic to get values from model using placeholder text.
      text.gsub!(placeholder, value)
    end

Any suggestions on how this can be fixed(made more efficient) or any alternate ways to do this? 关于如何解决(提高效率)的任何建议或其他替代方法可以做到这一点?

Thanks 谢谢

This was due to bad user data - string containing invalid values - which resulted gsub to work incorrectly and same string which was to be replaced by gsub was inserted back again. 这是由于错误的用户数据-包含无效值的字符串-导致gsub无法正常工作,并且再次插入了将被gsub替换的相同字符串。

And code had a recursive function which will continue replacing things until no string with [[xxx]] is present - this caused the code to go into a loop until memory was full. 并且代码具有递归功能,该功能将继续替换事物,直到没有带[[xxx]]的字符串为止-这导致代码进入循环直到内存已满。

"Kiprosh [[signature]]".gsub("[[signature]]", "\0 and \0")
expected =>  "Kiprosh \0 and \0"
actual   => "Kiprosh [[signature]] and [[signature]]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM