# \[Closed\] Can't sweep garbage object immediatly when GC\_ENABLE\_LAZY\_SWEEP=0

**URL:** <https://www.ruby-forum.com/t/closed-cant-sweep-garbage-object-immediatly-when-gc-enable-lazy-sweep-0/232768>\
**Category:** Development\
**Created:** [October 27, 2014, 5:41am UTC](https://www.ruby-forum.com/t/closed-cant-sweep-garbage-object-immediatly-when-gc-enable-lazy-sweep-0/232768 "2014-10-27T05:41:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [October 27, 2014, 5:41am UTC](https://www.ruby-forum.com/t/closed-cant-sweep-garbage-object-immediatly-when-gc-enable-lazy-sweep-0/232768/1 "2014-10-27T05:41:09Z")

</div>

Issue #10431 has been updated by Nobuyoshi N…

Status changed from Open to Closed  
% Done changed from 0 to 100

Applied in changeset r48150.

* * *

gc.c: fix no lazy sweep mode

- gc.c (gc\_sweep\_rest): sweep rest pages regardless of whether  
lazy sweep is enabled or not. based on the patch by Masahiro  
Ide at [ruby-dev:48706]. [Bug #10431]

* * *

Bug #10431: Can’t sweep garbage object immediatly when  
GC\_ENABLE\_LAZY\_SWEEP=0

> **[Bug #10431: Can't sweep garbage object immediatly when GC\_ENABLE\_LAZY\_SWEEP=0...](https://bugs.ruby-lang.org/issues/10431#change-49656)**
>
> Redmine

- Author: Masahiro Ide
- Status: Closed
- Priority: Normal
- Assignee:
- Category: core
- Target version: current: 2.2.0
- ruby -v: ruby 2.2.0dev (2014-10-26 trunk 48073) [x86\_64-darwin14]
- Backport: 2.0.0: DONTNEED, 2.1: DONTNEED

* * *

GC\_ENABLE\_LAZY\_SWEEP=0と設定してrubyをビルドし，以下のコードを  
実行した場合，GC時にオブジェクトが解放されていないように見えます．

```auto
$ ../configure CFLAGS="-DGC_ENABLE_LAZY_SWEEP=0" && make miniruby
$ cat bug2.rb
GC.disable
10000.times { Object.new }
total_freed_objects1 = GC.stat[:total_freed_objects]
GC.enable
GC.start(full_mark: true, immediate_sweep: true)
total_freed_objects2 = GC.stat[:total_freed_objects]
puts "freed object : #{total_freed_objects2 - total_freed_objects1}"

$ ./miniruby -v
ruby 2.2.0dev (2014-10-26 trunk 48073) [x86_64-darwin14]
$ ./miniruby bug2.rb
freed object : 0

```

また，rubyのコンパイルフラグをGC\_ENABLE\_LAZY\_SWEEP=0,  
RGENGC\_CHECK\_MODE=1と設定した上でビルドし，以下のコードを実行すると  
assertで落ちます．

```auto
$ ./miniruby -e '10000.times { Object.new }'
Assertion failed: (objspace->flags.stat == gc_stat_none), function 
gc_start, file ../gc.c, line 5802.
[1] 91336 abort ./miniruby -e '10000.times { Object.new }'

```

sweepフェーズにてGC\_ENABLE\_LAZY\_SWEEP!=1の時のみsweepが実行されるのが  
原因のようですので，これを修正するパッチを作りました．

```diff
diff --git a/gc.c b/gc.c
index a27cc01..979e2e0 100644
--- a/gc.c
+++ b/gc.c
@@ -3314,9 +3314,8 @@ static void
 gc_sweep_rest(rb_objspace_t *objspace)
 {
     rb_heap_t *heap = heap_eden; /* lazy sweep only for eden */
-
- if (is_lazy_sweeping(heap)) {
- while (is_lazy_sweeping(heap)) {
+ if ((heap)->sweep_pages != NULL) {
+ while ((heap)->sweep_pages != NULL) {
            gc_sweep_step(objspace, heap);
        }
     }

```

English:

When I tried to compile ruby with GC\_ENABLE\_LAZY\_SWEEP=0,  
Ruby does not sweep objects immediatly.

```auto
$ ../configure CFLAGS="-DGC_ENABLE_LAZY_SWEEP=0" && make miniruby
$ cat bug2.rb
GC.disable
10000.times { Object.new }
total_freed_objects1 = GC.stat[:total_freed_objects]
GC.enable
GC.start(full_mark: true, immediate_sweep: true)
total_freed_objects2 = GC.stat[:total_freed_objects]
puts "freed object : #{total_freed_objects2 - total_freed_objects1}"

$ ./miniruby -v
ruby 2.2.0dev (2014-10-26 trunk 48073) [x86_64-darwin14]
$ ./miniruby bug2.rb
freed object : 0

```

In addition, when I set GC\_ENABLE\_LAZY\_SWEEP=0 and RGENGC\_CHECK\_MODE=1  
ruby gives me assertion error like this:

```auto
$ ./miniruby -e '10000.times { Object.new }'
Assertion failed: (objspace->flags.stat == gc_stat_none), function 
gc_start, file ../gc.c, line 5802.
[1] 91336 abort ./miniruby -e '10000.times { Object.new }'

```

The following patch fix this problem.

```diff
diff --git a/gc.c b/gc.c
index a27cc01..979e2e0 100644
--- a/gc.c
+++ b/gc.c
@@ -3314,9 +3314,8 @@ static void
 gc_sweep_rest(rb_objspace_t *objspace)
 {
     rb_heap_t *heap = heap_eden; /* lazy sweep only for eden */
-
- if (is_lazy_sweeping(heap)) {
- while (is_lazy_sweeping(heap)) {
+ if ((heap)->sweep_pages != NULL) {
+ while ((heap)->sweep_pages != NULL) {
            gc_sweep_step(objspace, heap);
        }
     }

```
