Optimizing Ruby code is essential for better performance. This chapter covers profiling tools, garbage collection, memory management, and techniques to enhance Ruby program efficiency. The MCQs below will help assess your understanding.
benchmark gemmemory_profiler gemObjectSpacegc moduleBenchmark.measure method return?
Benchmark module in Ruby?
Benchmark.realtimeBenchmark.measureBenchmark.timeBenchmark.bmbmmemory_profiler gem in Ruby?
ObjectSpace.garbage_collectGC.startGC.collectObjectSpace.cleanupObjectSpace moduleGC moduleMemoryStats gemmemory_profiler gemGC.stat method return in Ruby?
eval to execute code dynamicallyArray#each instead of each_with_indexArray#map instead of eachmemory_profilerruby-profstackprofBenchmarkObjectSpace.memsize_ofGC.statObjectSpace.memory_sizeMemoryStats gemruby-prof gem used for in Ruby?
Benchmark.measurememory_profilerruby-profruby-prof gem provide that is useful for optimization?
Symbol instead of StringArray#map instead of loopsdef instead of lambdaeval#freeze on an object?
#dup method in Ruby?
GC.compact in Ruby?
ObjectSpace.each_object method allow you to do in Ruby?
ruby-profObjectSpace.each_objectmemory_profilergc.garbage_collectGC::Profiler module in Ruby?
| Qno | Answer (Option with text) |
|---|---|
| 1 | B) benchmark |
| 2 | B) using the memory_profiler gem |
| 3 | B) The time taken to execute the block of code |
| 4 | C) Benchmark.time |
| 5 | A) To find memory leaks in Ruby programs |
| 6 | B) It frees up memory by removing unreachable objects |
| 7 | B) GC.start |
| 8 | D) Using the memory_profiler gem |
| 9 | B) The garbage collection statistics |
| 10 | D) Generational Garbage Collection |
| 11 | B) To improve execution speed and efficiency |
| 12 | B) Avoid object creation in loops |
| 13 | B) Use lazy enumerators to process data on demand |
| 14 | A) Use Array#each instead of each_with_index |
| 15 | D) Use proper object references and rely on garbage collection |
| 16 | A) memory_profiler |
| 17 | A) Using ObjectSpace.memsize_of |
| 18 | C) To generate detailed performance profiles for method calls |
| 19 | D) All of the above |
| 20 | B) Detailed method call statistics and execution time |
| 21 | D) Use caching and memoization for frequent method calls |
| 22 | C) Use memoization to store results of expensive computations |
| 23 | A) It makes the object immutable, preventing modifications |
| 24 | A) Increased memory usage |
| 25 | B) By preventing unnecessary method re-evaluations |
| 26 | C) To optimize memory after garbage collection |
| 27 | B) Iterate through all objects in memory |
| 28 | B) ObjectSpace.each_object |
| 29 | A) By analyzing object references and marking unreachable objects |
| 30 | A) It tracks and provides garbage collection statistics |