-
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:-
- https://www.omniref.com/blog/blog/2014/03/27/ruby-garbage-collection-still-not-ready-for-production/
- http://patshaughnessy.net/2013/10/24/visualizing-garbage-collection-in-ruby-and-python
- http://patshaughnessy.net/2012/1/4/never-create-ruby-strings-longer-than-23-characters
- http://rubini.us/2013/06/22/concurrent-garbage-collection/
- http://samsaffron.com/archive/2014/04/08/ruby-2-1-garbage-collection-ready-for-production
- http://www.infoq.com/news/2014/12/ruby-2.2.0-released http://patshaughnessy.net/2012/3/23/why-you-should-be-excited-about-garbage-collection-in-ruby-2-0
Sounds like you are selling GC. I'm sold... :)