# Rapid array accesses and garbage collection

**URL:** <https://rubytalk.org/t/rapid-array-accesses-and-garbage-collection/74846>\
**Category:** ruby-talk\
**Created:** [21 November 2018 11:14 UTC](https://rubytalk.org/t/rapid-array-accesses-and-garbage-collection/74846 "2018-11-21T11:14:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Hickman2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/peter_hickman2/32/1832_2.png) [@Peter\_Hickman2](https://rubytalk.org/u/Peter_Hickman2)\
**Post date:** [21 November 2018 11:14 UTC](https://rubytalk.org/t/rapid-array-accesses-and-garbage-collection/74846/1 "2018-11-21T11:14:37Z")

</div>

I have a small app that uses an Array as a queue. Works well enough but  
(seems) to suffer from GC lockouts

Items are added and removed (\<\< and shift) from the Array frequently and  
the queue itself rarely gets very large, 10s to 100s of records, but the  
throughput is 100,000s of records

Which is, I assume, causing it to periodically freeze. The question is:

1) Should I manually start GC after every shift operation. Will this trade  
off frequent small GC interruptions for less big freezes  
2) Should I manually start GC after X shifts. Will large GC events be more  
efficient  
3) Should I create a fake Array such as below and have much larger GC events

Any pointers or will it just be trying stuff and measuring?

class SlowArray  
&nbsp;&nbsp;DEAD\_ZONE = 5

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@data = []  
&nbsp;&nbsp;&nbsp;&nbsp;@size = 0  
&nbsp;&nbsp;&nbsp;&nbsp;@read\_from = 0  
&nbsp;&nbsp;end

&nbsp;&nbsp;def \<\<(value)  
&nbsp;&nbsp;&nbsp;&nbsp;@data \<\< value  
&nbsp;&nbsp;&nbsp;&nbsp;@size += 1  
&nbsp;&nbsp;end

&nbsp;&nbsp;def shift  
&nbsp;&nbsp;&nbsp;&nbsp;if @size == 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return nil  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = @data[@read\_from]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@read\_from += 1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @read\_from == DEAD\_ZONE  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data = @data[DEAD\_ZONE..-1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GC.start  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@read\_from = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@size -= 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def size  
&nbsp;&nbsp;&nbsp;&nbsp;@size  
&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/peter_hickman2/32/1832_2.png) [@Peter\_Hickman2](https://rubytalk.org/u/Peter_Hickman2)\
**Post date:** [26 November 2018 14:38 UTC](https://rubytalk.org/t/rapid-array-accesses-and-garbage-collection/74846/2 "2018-11-26T14:38:18Z")

</div>

For reference, frequent small GC runs stops the long freeze-up that can  
occur but slows down the throughput
