{"id":3455,"date":"2018-08-21T08:09:36","date_gmt":"2018-08-20T23:09:36","guid":{"rendered":"https:\/\/blog.timecrowd.net\/?p=3455"},"modified":"2018-09-03T10:55:39","modified_gmt":"2018-09-03T01:55:39","slug":"memory-leak-on-rails","status":"publish","type":"post","link":"https:\/\/blog.timecrowd.net\/memory-leak-on-rails\/","title":{"rendered":"Rails\u30a2\u30d7\u30ea\u306e\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>\u6700\u8fd1\u53c2\u52a0\u3057\u3066\u3044\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u3069\u3046\u3084\u3089\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u304c\u767a\u751f\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u8abf\u67fb\u3057\u305f\u306e\u3067\u3001\u305d\u306e\u3068\u304d\u306b\u8abf\u3079\u305f\u8abf\u67fb\u65b9\u6cd5\u306a\u3069\u3092\u307e\u3068\u3081\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u590f\u306b\u5317\u6d77\u9053\u306b\u884c\u304d\u305f\u3044\u306a\u3041\u3068\u601d\u3063\u3066\u3044\u306a\u304c\u3089\u884c\u3051\u306a\u304b\u3063\u305f\u65c5\u3057\u305f\u304b\u3063\u305f\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u4e09\u5b85\u3067\u3059\u3002<\/p>\n<h3>\u4eca\u56de\u306e\u958b\u767a\u74b0\u5883<\/h3>\n<p>\u307e\u305a\u306f\u4eca\u56de\u306e\u304a\u8a71\u306b\u95a2\u4fc2\u3057\u305d\u3046\u306a\u767b\u5834\u4eba\u7269\u3060\u3051\u7d39\u4ecb\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>Ruby 2.4.4<\/li>\n<li>Rails 4.2.10<\/li>\n<li>Sidekiq 5.1.3<\/li>\n<\/ul>\n<h3>\u305d\u3082\u305d\u3082\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3063\u3066\u306a\u3093\u3067\u3057\u3087\u3046\uff1f<\/h3>\n<blockquote><p><span style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; font-size: 1rem;\">\u30e1\u30e2\u30ea\u30ea\u30fc\u30af (Memory leak) \u3068\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306b\u304a\u3051\u308b\u30d0\u30b0\u306e\u4e00\u7a2e\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u78ba\u4fdd\u3057\u305f\u30e1\u30e2\u30ea\u306e\u4e00\u90e8\u3001\u307e\u305f\u306f\u5168\u90e8\u3092\u89e3\u653e\u3059\u308b\u306e\u3092\u5fd8\u308c\u3001\u78ba\u4fdd\u3057\u305f\u307e\u307e\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3053\u3068\u3092\u8a00\u3046\u3002<\/span><strong style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; font-size: 1rem;\">\u30d7\u30ed\u30b0\u30e9\u30de\u306b\u3088\u308b\u5358\u7d14\u306a\u30df\u30b9\u3084\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u8ad6\u7406\u7684\u6b20\u9665\u306b\u3088\u3063\u3066\u767a\u751f\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u3002<\/strong><span style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; font-size: 1rem;\">\uff08\u592a\u5b57\u306f\u7b46\u8005\u306b\u3088\u308b\uff09<\/span><\/p>\n<p><a href=\"https:\/\/ja.wikipedia.org\/wiki\/%E3%83%A1%E3%83%A2%E3%83%AA%E3%83%AA%E3%83%BC%E3%82%AF\" target=\"_blank\" rel=\"noopener\">https:\/\/ja.wikipedia.org\/wiki\/%E3%83%A1%E3%83%A2%E3%83%AA%E3%83%AA%E3%83%BC%E3%82%AF<\/a><\/p><\/blockquote>\n<p>\u8f9b\u3044&#8230;\u8f9b\u3059\u304e\u308b\u3002<\/p>\n<p>\u3044\u3084\u78ba\u304b\u306b\u305d\u3046\u306a\u3093\u3067\u3059\u3051\u3069\u3001\u3084\u3063\u3061\u3083\u3046\u3093\u3067\u3059\u3088\u3001\u3060\u3063\u3066\u4eba\u9593\u3060\u3082\u306e\u3002<\/p>\n<p>\u305f\u3060Ruby\u306f\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30bf\u304c\u826f\u3057\u306a\u306b\u3057\u3066\u304f\u308c\u3066\u3044\u308b\u3093\u3058\u3083\u306a\u3044\u306e\u304b\u3068\u601d\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u307e\u305a\u306f\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30bf\u304c\u52d5\u3044\u3066\u3044\u3066\u3001\u3046\u307e\u304f\u30e1\u30e2\u30ea\u304c\u958b\u653e\u3055\u308c\u3066\u3044\u308b\u306e\u304b\u3068\u3044\u3046\u3068\u3053\u308d\u304b\u3089\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u8abf\u3079\u3066\u307f\u308b\u3068\u4e00\u756a\u304a\u624b\u8efd\u306a\u306e\u306f<code>ObjectSpace::memsize_of_all<\/code>\u3092\u4f7f\u3046\u65b9\u6cd5\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<pre><code>require 'objspace'\r\n\r\nArray.new(100_000, 'ruby')\r\nputs \"#{ObjectSpace.memsize_of_all * 0.001 * 0.001} MB\"\r\n=&gt; 3.579259 MB<\/code><\/pre>\n<p>\u3053\u306e<code>ObjectSpace::memsize_of_all<\/code>\u3063\u3066\u306e\u304c\u4f55\u3092\u3084\u3063\u3066\u3044\u308b\u304b\u3068\u8a00\u3046\u3068\u3001\u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u3067\u306f<code>ObjectSpace::each_object<\/code>\u3092\u3076\u3093\u56de\u3057\u3066\u3001\u3053\u308c\u307e\u305f<code>ObjectSpace::memsize_of<\/code>\u3067\u5404\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30b5\u30a4\u30ba\u3092\u5408\u8a08\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3082\u306e\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><code>ObjectSpace::memsize_of<\/code>\u306fsizeof(RVALUE)\u3057\u305f\u7d50\u679c\u304c\u8fd4\u3055\u308c\u3001RVALUE\u306f64bit\u30b7\u30b9\u30c6\u30e0\u306e\u5834\u5408\u306f40\u30d0\u30a4\u30c8\u3060\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f<code>ObjectSpace::memsize_of_all<\/code>\u306b\u3057\u3066\u3082\u3001<code>ObjectSpace::memsize_of<\/code>\u306b\u3057\u3066\u3082\u3001\u6b63\u78ba\u306a\u6570\u5024\u3067\u306f\u306a\u3044\u305f\u3081\u3001\u53c2\u8003\u7a0b\u5ea6\u3068\u3059\u308b\u3088\u3046\u306b\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306b\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote><p>Note that the returned size is incomplete. You need to deal with this information as only a HINT. Especially, the size of T_DATA may not be correct.<\/p>\n<p><a href=\"http:\/\/ruby-doc.org\/stdlib-2.5.1\/libdoc\/objspace\/rdoc\/ObjectSpace.html#method-c-memsize_of_all\" target=\"_blank\" rel=\"noopener\">http:\/\/ruby-doc.org\/stdlib-2.5.1\/libdoc\/objspace\/rdoc\/ObjectSpace.html#method-c-memsize_of_all<\/a><\/p><\/blockquote>\n<p>\u3068\u306f\u8a00\u3048\u3001\u30e1\u30e2\u30ea\u304c\u958b\u653e\u3055\u308c\u306a\u3044\u307e\u307e\u306b\u306a\u3063\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u304f\u3089\u3044\u306f\u308f\u304b\u308a\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\uff08\u64ec\u4f3c\u30b3\u30fc\u30c9\uff09\u3067\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u524d\u5f8c\u3067\u91cf\u304c\u5909\u308f\u3063\u3066\u3044\u308b\u306e\u304b\u691c\u8a3c\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre><code>require 'objspace'\r\n\r\n\r\n# code ...\r\nputs \"Before: #{ObjectSpace.memsize_of_all * 0.001 * 0.001} MB\"\r\nGC.start\r\nputs \"After: #{ObjectSpace.memsize_of_all * 0.001 * 0.001} MB\"<\/code><\/pre>\n<p>\u7d50\u679c\u3092\u898b\u308b\u3068\u30e1\u30e2\u30ea\u304c\u958b\u653e\u3055\u308c\u305a\u306b\u6b8b\u3063\u3066\u3044\u308b\u3088\u3046\u306b\u306f\u898b\u3048\u305a\u3001\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u5f8c\u306f\u30e1\u30e2\u30ea\u306e\u4f7f\u7528\u91cf\u3082\u3061\u3083\u3093\u3068\u6e1b\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<h3>malloc\u3067\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30e1\u30e2\u30ea\u304c\u500d\u5897\uff1f<\/h3>\n<p>Ruby: malloc\u3067\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u30e1\u30e2\u30ea\u304c\u500d\u5897\u3059\u308b\u7406\u7531\uff08\u7ffb\u8a33\uff09<br \/>\n<a href=\"https:\/\/techracho.bpsinc.jp\/hachi8833\/2017_12_28\/50109\" target=\"_blank\" rel=\"noopener\">https:\/\/techracho.bpsinc.jp\/hachi8833\/2017_12_28\/50109<\/a><\/p>\n<p>\u4e0a\u8a18\u306e\u8a18\u4e8b\u306b\u3088\u308b\u3068\u3001\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9Ruby\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306f\u30e1\u30e2\u30ea\u306e\u65ad\u7247\u5316\uff08\u307e\u305f\u306f\u30a2\u30ed\u30b1\u30fc\u30b7\u30e7\u30f3\uff09\u306f<code>ObjectSpace<\/code>\u306e\u4e2d\u3067\u8d77\u304d\u3066\u3044\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u307b\u3068\u3093\u3069\u304c<code>malloc<\/code>\u306b\u3088\u3063\u3066\u8d77\u3053\u3063\u3066\u3044\u308b\u3082\u306e\u3060\u3068\u3044\u3046\u3053\u3068\u306e\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u4e8b\u4ef6\u306f\u4f1a\u8b70\u5ba4\u3067\u8d77\u304d\u3066\u3044\u308b\u306e\u3067\u306f\u306a\u3044\u3001\u73fe\u5834\u3067&#8230;\uff01\u3063\u3066\u3084\u3064\u3067\u3059\u304b\u306d\u3002\uff08\u9055\u3046<\/p>\n<p>\u305d\u3057\u3066\u3053\u306e\u554f\u984c\u3092\u7de9\u548c\u3059\u308b\u305f\u3081\u306b<code>MALLOC_ARENA_MAX<\/code>\u3068\u3044\u3046\u74b0\u5883\u5909\u6570\u3092\u5229\u7528\u3057\u3066\u30b9\u30ec\u30c3\u30c9\u5358\u4f4d\u30e1\u30e2\u30ea\u30a2\u30ea\u30fc\u30ca\u3068\u3044\u3046\u306e\u3092\u5236\u9650\u3059\u308b\u3068\u30e1\u30e2\u30ea\u306e\u65ad\u7247\u5316\u3092\u6539\u5584\u3067\u304d\u308b\u3068\u3044\u3046\u3053\u3068\u306e\u3088\u3046\u3067\u3059\u3002\u305f\u3060\u3053\u308c\u306b\u3082\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u306e\u95a2\u4fc2\u3068\u3057\u3066\u30ec\u30b9\u30dd\u30f3\u30b9\u30b9\u30d4\u30fc\u30c9\u304c\u52a3\u5316\u3059\u308b\u3053\u3068\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f\u306f<code>malloc<\/code>\u306e\u4ee3\u308f\u308a\u306b<code>jemalloc<\/code>\u3092Ruby\u3067\u4f7f\u3046\u3088\u3046\u306b\u3059\u308b\u3068\u30e1\u30e2\u30ea\u306e\u65ad\u7247\u5316\u3092\u6291\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u3057\u3063\u304b\u308a\u3068\u7406\u89e3\u3067\u304d\u3066\u3044\u306a\u3044\u90e8\u5206\u3082\u591a\u3044\u306e\u3067\u3001\u8a73\u3057\u304f\u306f\u8a18\u4e8b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u3053\u306e\u3042\u305f\u308a\u306f\u307e\u3060\u8a66\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u4eca\u306e\u307e\u307e\u89e3\u6c7a\u306e\u7cf8\u53e3\u304c\u898b\u3048\u306a\u3044\u307e\u307e\u3068\u3044\u3046\u3053\u3068\u3067\u3042\u308c\u3070\u3001\u4e00\u5ea6\u8a66\u3057\u3066\u307f\u3088\u3046\u304b\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u307e\u3068\u3081<\/h3>\n<p>\u4eca\u56de\u521d\u3081\u3066\u3053\u3053\u307e\u3067\u5185\u90e8\u306b\u3064\u3044\u3066\u8abf\u3079\u305f\u306e\u3067\u3001\u307e\u3060\u308f\u304b\u3089\u306a\u3044\u3053\u3068\u3060\u3089\u3051\u3067\u3059\u304c\u3001\u4eca\u307e\u3067\u306b\u306f\u306a\u3044\u7d4c\u9a13\u304c\u3067\u304d\u3066\u975e\u5e38\u306b\u6709\u610f\u7fa9\u306b\u611f\u3058\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3060\u3051\u3067\u306f\u306a\u304f\u3001\u3053\u3046\u3044\u3063\u305f\u8a00\u8a9e\u5185\u90e8\u306e\u3053\u3068\u307e\u3067\u7406\u89e3\u3057\u3066\u77e5\u8b58\u3068\u3057\u3066\u81ea\u5206\u306e\u9053\u5177\u7bb1\u306b\u7d0d\u3081\u3089\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u305f\u3044\u3082\u306e\u3067\u3059\u306d\uff01<\/p>\n<p>\u307e\u305f\u65b0\u305f\u306b\u89e3\u6c7a\u65b9\u6cd5\u306a\u3069\u304c\u898b\u3064\u304b\u308a\u307e\u3057\u305f\u3089\u3001\u3053\u306e\u5834\u3067\u7686\u3055\u3093\u306b\u5171\u6709\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6700\u8fd1\u53c2\u52a0\u3057\u3066\u3044\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u3069\u3046\u3084\u3089\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u304c\u767a\u751f\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u8abf\u67fb\u3057\u305f\u306e\u3067\u3001\u305d\u306e\u3068\u304d\u306b\u8abf\u3079\u305f\u8abf\u67fb\u65b9\u6cd5\u306a\u3069\u3092\u307e\u3068\u3081\u305f\u3044\u3068\u601d\u3044\u307e\u3059&#8230;","protected":false},"author":5,"featured_media":3541,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cybocfi_hide_featured_image":""},"categories":[5],"tags":[12],"_links":{"self":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts\/3455"}],"collection":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/comments?post=3455"}],"version-history":[{"count":12,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts\/3455\/revisions"}],"predecessor-version":[{"id":3734,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/posts\/3455\/revisions\/3734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/media\/3541"}],"wp:attachment":[{"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/media?parent=3455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/categories?post=3455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.timecrowd.net\/wp-json\/wp\/v2\/tags?post=3455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}