You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Compare two same versions of code, one with GC works and one with GC disable
require"benchmark"num_rows=100000num_cols=10data=Array.new(num_rows){Array.new(num_cols){"x"*1000}}GC.disable# just add disable GC in version 2time=Benchmark.realtimedocsv=data.map{ |row| row.join(",")}.join("\n")endputstime.round(2)
Now let’s run this and compare our measurements between 2 versions. On my computer, version 1 takes 2.72s and version 2 takes 1.91s. The percent of time spent in GC is about 30%. We can see our program spends quite a lot of its execution time in the garbage collector.
Of course, we can not disable GC altogether for better performance. Turning off GC significantly increases peak memory consumption. The operating system may run out of memory or start swapping.
We will use RSS to measure object memory before and after GC kick-in
In Ruby everything is an object that’s internally represented as the RVALUE struct. sizeof(RVALUE) is machine dependent:
For example, 40 bytes in 64-bit architecture. Most modern computers are 64-bit, so we’ll assume that one object costs us 40 bytes of memory to create.
A Ruby object can store only a limited amount of data, up to 40 bytes. Slightly less than half of that is required for upkeep. All data that does not fit into the object itself is dynamically allocated outside of the Ruby heap. When the object is swept by GC, the memory is freed.
ObjectSpace library
For example, a Ruby string stores only 23 bytes in the RSTRING object on a 64-bit system. When the string length becomes larger than 23 bytes, Ruby allocates additional memory for it.
We can see how much by calling ObjectSpace#memsize_of, for example, like this:
1. Memory consumption and GC
Print Process RSS
On Linux and Mac OS X you can get RSS from the ps command:
Garbage Collector
Compare two same versions of code, one with GC works and one with GC disable
Now let’s run this and compare our measurements between 2 versions. On my computer, version 1 takes 2.72s and version 2 takes 1.91s. The percent of time spent in GC is about 30%. We can see our program spends quite a lot of its execution time in the garbage collector.
Of course, we can not disable GC altogether for better performance. Turning off GC significantly increases peak memory consumption. The operating system may run out of memory or start swapping.
We will use RSS to measure object memory before and after GC kick-in
2. Object Memory
Objects
In Ruby everything is an object that’s internally represented as the
RVALUE
struct.sizeof(RVALUE)
is machine dependent:For example, 40 bytes in 64-bit architecture. Most modern computers are 64-bit, so we’ll assume that one object costs us 40 bytes of memory to create.
A Ruby object can store only a limited amount of data, up to 40 bytes. Slightly less than half of that is required for upkeep. All data that does not fit into the object itself is dynamically allocated outside of the Ruby heap. When the object is swept by GC, the memory is freed.
ObjectSpace library
For example, a Ruby string stores only 23 bytes in the
RSTRING
object on a 64-bit system. When the string length becomes larger than 23 bytes, Ruby allocates additional memory for it.We can see how much by calling
ObjectSpace#memsize_of
, for example, like this:https://ivoanjo.me/blog/2021/02/11/looking-into-array-memory-usage/
The text was updated successfully, but these errors were encountered: