# Backported GC.stress for 1.8

**URL:** <https://www.ruby-forum.com/t/backported-gc-stress-for-1-8/126736>\
**Category:** Development\
**Created:** [January 21, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/backported-gc-stress-for-1-8/126736 "2008-01-21T20:12:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sugie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@sugie](https://www.ruby-forum.com/u/sugie)\
**Post date:** [January 21, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/backported-gc-stress-for-1-8/126736/1 "2008-01-21T20:12:35Z")

</div>

e$B:XF#$H?=$7$^$9!#e(B

GC.stresse$B$re(B1.8e$B%V%i%s%A$K9g$&$h$&!“%Q%C%A$r:n$j$^$7$?!#e(B  
1.8e$B7ONs$N3+H/$K$bM-MQ$G$”$k$N$OJQ$o$j$J$$$H;W$&$N$G!"e(B1.8.7e$B$K8~$1$FDI2C$re(B  
e$B9M$($F$$$?$@$1$k$H$&$l$7$$$G$9!#e(B

# Index: signal.c

— signal.c (revision 15162)  
+++ signal.c (working copy)  
@@ -622,6 +622,8 @@  
}  
#endif

- extern int ruby\_gc\_stress;
- ruby\_gc\_stress = 0;  
rb\_bug(“Segmentation fault”);  
}  
#endif  
Index: gc.c  
===================================================================  
— gc.c (revision 15162)  
+++ gc.c (working copy)  
@@ -87,6 +87,39 @@  
rb\_exc\_raise(nomem\_error);  
}

+/\*

- 
  - call-seq:

- 
  - GC.stress =\> true or false

- 
  - 

- 
  - returns current status of GC stress mode.

- \*/
- 

+static VALUE  
+gc\_stress\_get(VALUE self)  
+{

- return ruby\_gc\_stress ? Qtrue : Qfalse;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - GC.stress = bool =\> bool

- 
  - 

- 
  - updates GC stress mode.

- 
  - 

- 
  - When GC.stress = true, GC is invoked for all GC opportunity:

- 
  - all memory and object allocation.

- 
  - 

- 
  - Since it makes Ruby very slow, it is only for debugging.

- \*/
- 

+static VALUE  
+gc\_stress\_set(VALUE self, VALUE bool)  
+{

- rb\_secure(2);
- ruby\_gc\_stress = RTEST(bool);
- return bool;  
+}
- 

void \*  
ruby\_xmalloc(size)  
long size;  
@@ -99,7 +132,7 @@  
if (size == 0) size = 1;  
malloc\_increase += size;

- if (malloc\_increase \> malloc\_limit) {

- if (ruby\_gc\_stress || malloc\_increase \> malloc\_limit) {  
garbage\_collect();  
}  
RUBY\_CRITICAL(mem = malloc(size));  
@@ -139,6 +172,7 @@  
if (!ptr) return xmalloc(size);  
if (size == 0) size = 1;  
malloc\_increase += size;
- if (ruby\_gc\_stress) garbage\_collect();  
RUBY\_CRITICAL(mem = realloc(ptr, size));  
if (!mem) {  
garbage\_collect();  
@@ -378,7 +412,7 @@  
{  
VALUE obj;

- if (!freelist) garbage\_collect();

- if (ruby\_gc\_stress || !freelist) garbage\_collect();

+int ruby\_gc\_stress = 0;  
+  
#ifdef DJGPP  
/\* set stack size ([http://www.delorie.com/djgpp/v2faq/faq15\_9.html](http://www.delorie.com/djgpp/v2faq/faq15_9.html)) _/  
unsigned int \_stklen = 0x180000; /_ 1.5 kB \*/  
@@ -2032,6 +2068,8 @@  
rb\_define\_singleton\_method(rb\_mGC, “start”, rb\_gc\_start, 0);  
rb\_define\_singleton\_method(rb\_mGC, “enable”, rb\_gc\_enable, 0);  
rb\_define\_singleton\_method(rb\_mGC, “disable”, rb\_gc\_disable, 0);

- rb\_define\_singleton\_method(rb\_mGC, “stress”, gc\_stress\_get, 0);

- rb\_define\_singleton\_method(rb\_mGC, “stress=”, gc\_stress\_set, 1);  
rb\_define\_method(rb\_mGC, “garbage\_collect”, rb\_gc\_start, 0);

---

<div class="post-metadata">

**Author:** ![sugie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@sugie](https://www.ruby-forum.com/u/sugie)\
**Post date:** [January 21, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/backported-gc-stress-for-1-8/126736/2 "2008-01-21T20:19:40Z")

</div>

e$B:XF#$G$9!#e(B

On Tue, 22 Jan 2008 04:12:12 +0900  
Tadashi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> GC.stresse$B$re(B1.8e$B%V%i%s%A$K9g$&$h$&!"%Q%C%A$r:n$j$^$7$?!#e(B

e$B$9$_$^$;$s!"@hDx$N$b$N$K$OK^%_%9$,$"$j$^$7$?!#L5;k$7$F2\<$5$$!#e(B

# Index: signal.c

— signal.c (revision 15162)  
+++ signal.c (working copy)  
@@ -622,6 +622,8 @@  
}  
#endif

- extern int ruby\_gc\_stress;
- ruby\_gc\_stress = 0;  
rb\_bug(“Segmentation fault”);  
}  
#endif  
Index: gc.c  
===================================================================  
— gc.c (revision 15162)  
+++ gc.c (working copy)  
@@ -74,6 +74,8 @@  
static VALUE nomem\_error;  
static void garbage\_collect();

+int ruby\_gc\_stress = 0;  
+  
void  
rb\_memerror()  
{  
@@ -87,6 +89,39 @@  
rb\_exc\_raise(nomem\_error);  
}

+/\*

- 
  - call-seq:

- 
  - GC.stress =\> true or false

- 
  - 

- 
  - returns current status of GC stress mode.

- \*/
- 

+static VALUE  
+gc\_stress\_get(VALUE self)  
+{

- return ruby\_gc\_stress ? Qtrue : Qfalse;  
+}
- 

+/\*

- 
  - call-seq:

- 
  - GC.stress = bool =\> bool

- 
  - 

- 
  - updates GC stress mode.

- 
  - 

- 
  - When GC.stress = true, GC is invoked for all GC opportunity:

- 
  - all memory and object allocation.

- 
  - 

- 
  - Since it makes Ruby very slow, it is only for debugging.

- \*/
- 

+static VALUE  
+gc\_stress\_set(VALUE self, VALUE bool)  
+{

- rb\_secure(2);
- ruby\_gc\_stress = RTEST(bool);
- return bool;  
+}
- 

void \*  
ruby\_xmalloc(size)  
long size;  
@@ -99,7 +134,7 @@  
if (size == 0) size = 1;  
malloc\_increase += size;

- if (malloc\_increase \> malloc\_limit) {

- if (ruby\_gc\_stress || malloc\_increase \> malloc\_limit) {  
garbage\_collect();  
}  
RUBY\_CRITICAL(mem = malloc(size));  
@@ -139,6 +174,7 @@  
if (!ptr) return xmalloc(size);  
if (size == 0) size = 1;  
malloc\_increase += size;
- if (ruby\_gc\_stress) garbage\_collect();  
RUBY\_CRITICAL(mem = realloc(ptr, size));  
if (!mem) {  
garbage\_collect();  
@@ -378,7 +414,7 @@  
{  
VALUE obj;

- if (!freelist) garbage\_collect();

- if (ruby\_gc\_stress || !freelist) garbage\_collect();

- rb\_define\_singleton\_method(rb\_mGC, “stress”, gc\_stress\_get, 0);

- rb\_define\_singleton\_method(rb\_mGC, “stress=”, gc\_stress\_set, 1);  
rb\_define\_method(rb\_mGC, “garbage\_collect”, rb\_gc\_start, 0);
