JRuby - How to start the garbage collector?

I launched the JRuby irb console and typed:

irb(main):037:0* GC.enable
(irb):37 warning: GC.enable does nothing on JRuby
=> true
irb(main):038:0> GC.start
=> nil
irb(main):039:0> 

How to manually enable or run JVM trash during a program?

I ask because I have a program that should generate about 500 MB of test data and save it in MySQL. The program uses about 5 levels of nested loops, and it crashes due to the exception of the JVM memory heap after generating about 100 MB of test data, because there is no more heap memory. I would like to let the garbage collector run after each start of the outer loop, so that all orphaned objects created in the inner loops can be cleaned up.

+5
source share
2 answers

The exact answer to your question is:

require 'java'

java_import 'java.lang.System'

# ...

System.gc()

, , JVM GC, - JVM. .

, , , , , GC . :

class Foo; end

sleep(5)

ary = []
100_000.times { 100_000.times{  ary << Foo.new }; puts 'Done'; ary = [] }

jruby -J-verbose:gc foo.rb, , GC ; , JVisualVM (sleep - Jruby JVisualVM).

, , : -J-Xmx256m; . JRuby wiki.

: , GC, DevOps, .

+10

, Gc JVM. , , . , , .

-1

All Articles