From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 0AC251F4C0 for ; Wed, 23 Oct 2019 23:43:48 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 595E2120B0B; Thu, 24 Oct 2019 08:43:38 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 756D4120B08 for ; Thu, 24 Oct 2019 08:43:35 +0900 (JST) Received: by filter0046p3iad2.sendgrid.net with SMTP id filter0046p3iad2-4077-5DB0E5AA-32 2019-10-23 23:43:38.430634646 +0000 UTC m=+102198.499542228 Received: from herokuapp.com (unknown [52.90.27.11]) by ismtpd0015p1iad1.sendgrid.net (SG) with ESMTP id PGn-FoSAQV-fFeAr9-N1gw for ; Wed, 23 Oct 2019 23:43:38.346 +0000 (UTC) Date: Wed, 23 Oct 2019 23:43:38 +0000 (UTC) From: mame@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71112 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16278 X-Redmine-Issue-Author: cristiangreco X-Redmine-Sender: mame X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?EJh2gqwnyqXtd++xo=2FinyA1V0bXouTB4FkWnzNiKb4=2FnER+4tvUoMmxbIS4UcD?= =?us-ascii?Q?JkyBv7DPA9tFglwxYLMgVXogyTbv+=2FyK1FlR7hA?= =?us-ascii?Q?Ni83XFSey0h3BjFR=2FYuRjCxvsWxczqvXhOIjXrs?= =?us-ascii?Q?PjwrMJu1I5CjtFVUXuSjsY=2FrS4i1kd+KE6BqtRB?= =?us-ascii?Q?ufl8vAWm+WIiV=2FHd2Lsz7mQFA25IlmiPFcA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95520 Subject: [ruby-core:95520] [Ruby master Bug#16278] Potential memory leak when an hash is used as a key for another hash X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #16278 has been updated by mame (Yusuke Endoh). Status changed from Open to Rejected Ruby uses so-called conservative GC, which is inexact. It is not guaranteed that an object is collected when it becomes unreachable. You can confirm that it does not cause leak by the following code: ```ruby def create h = {{:a => 1} => 2} h.keys.first.object_id end loop { create } ``` This code uses constant memory. If it caused memory leak, the memory usage would continue to increase. ---------------------------------------- Bug #16278: Potential memory leak when an hash is used as a key for another hash https://bugs.ruby-lang.org/issues/16278#change-82293 * Author: cristiangreco (Cristian Greco) * Status: Rejected * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin18] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- Hi, I've been hitting what seems to be a memory leak. When an hash is used as key for another hash, the former object will be retained even after multiple GC runs. The following code snippet demonstrates how the hash `{:a => 1}` (which is never used outside the scope of `create`) is retained even after 10 GC runs (`find` will look for an object with a given `object_id` on heap). ```ruby # frozen_string_literal: true def create h = {{:a => 1} => 2} h.keys.first.object_id end def find(object_id) ObjectSpace.each_object(Hash).any?{|h| h.object_id == object_id} ? 1 : 0 end leaked = create 10.times do GC.start(full_mark: true, immediate_sweep: true) end exit find(leaked) ``` This code snippet is expected to exit with `0` while it exits with `1` in my tests. I've tested this on multiple recent ruby versions and OSs, either locally (OSX with homebrew) or in different CIs (e.g. [here](https://github.com/cristiangreco/ruby-hash-leak/commit/285e586b7193104989f59b92579fe8f25770141e/checks?check_suite_id=278711566)). Can you please help understand what's going on here? Thanks! -- https://bugs.ruby-lang.org/