No, there is no way to do it in Ruby.
There is a method called
GC::start, and the documentation even says:
Initiates garbage collection, even if manually disabled.
But that is not true.
GC::start is simply an advisory from your code to the runtime that it would be safe for your application to run a garbage collection now. But that is only a suggestion. The runtime is free to ignore this suggestion.
The majority of programming languages with automatic memory management do not give the programmer control over the garbage collector.
If Ruby had a method to force a garbage collection, then it would be impossible to implement Ruby on the JVM and neither JRuby nor TruffleRuby could exist, it would be impossible to implement Ruby on .NET and IronRuby couldn’t exist, it would be impossible to implement Ruby on ECMAScript and Opal couldn’t exist, it would be impossible to implement Ruby using existing high-performance garbage collectors and RubyOMR couldn’t exist.
Since it is in generally desirable to give implementors freedom to implement optimizations and make the language faster, languages are very cautious on specifying features that so drastically restrict what an implementor can do.
I am quite surprised that R has such a feature, especially since that means it is impossible to implement high-performance implementations like FastR in a way that is compliant with the language specification. FastR is up to more than 35× faster than GNU R, so it is obvious why it is desirable for something like FastR to exist. But one of the ways FastR is faster, is that it uses a third-party high-performance garbage collected runtime (either the GraalVM or the JVM) that does not allow control over garbage collection, and thus FastR can never be a compliant R implementation.
Interestingly, the documentation of
gc() has this to say:
[T]he primary purpose of calling
gcis for the report on memory usage.
CLICK HERE to find out more related problems solutions.