Skip to content

Instantly share code, notes, and snippets.

@psahni
Last active August 29, 2015 14:10
Show Gist options
  • Save psahni/ff9693b9aeeed685aa03 to your computer and use it in GitHub Desktop.
Save psahni/ff9693b9aeeed685aa03 to your computer and use it in GitHub Desktop.
RubyGarbageCollector
  • Garbage collector reclaims the memory occupied by the objects that are no longer used.

  • ObjectSpace class: We can track the memory usage by a datatype. Example count = ObjectSpace.each_object(Numeric) {|x| p x }

  • Gc works by deallocating a major collection of 'old' objects. The deallocation occurs when Ruby heap crosses certain thershold

  • We should not create big strings, becz the process start consuming more memory if it runs for long enough.

  • When we create large hashes, arrays, strings there is a high chance that you process will run out of memory soon.

  • Compare garbage collector with a beating heart in your body, if the heart stops functioning or it does not functions properly, you will not survive. Similarly, if garbage collector does not clean objects and create space for the 'new objects', then your application will tend to stop working and gradually die.

  • Ruby GC works by providing a free list of memory spaces, as the objects are created a space is allocated to them, now when a collection of unused objects are created then GC cleans it and adds to the list of free spaces.

  • Its like you put the dirty dishes into the sink and when you find the good collection of dirty dishes you are start cleaning. The other way can be you clean the dish as soon as you put into sink, this way is followed in Python GC.

  • When GC runs, you program stops and waits for GC to finish, but this is very fast.

  • Concurrent GC: Rubinius and JVM are using concurrent garbage collection which means that GC runs simultaneously at the time of your program runs, hence our program does not have to stop which a GC runs. The concurrent GC runs in a separate thread.

  • In ruby 2.1, we are introduced a concept of generational garbage collection.

  • In ruby 2.1, a min and max 'malloc' point is set like 8Mb, 16Mb. This means if your program memory grows above min allocated memory, the GC starts to run. In irb - do GC.stat

  • Performance of ruby 2.1 has been improved becaz of better memory allocation settings.

References:-

@v1bh0r
Copy link

v1bh0r commented Dec 4, 2014

Sounds like you are selling GC. I'm sold... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment