Skip to content

Commit 7b8d196

Browse files
committedJun 1, 2018
GC part already loaded from native -> at least delay JI loading on use
... we simply generate the Java listener on demand as its to be used
1 parent dcd5d62 commit 7b8d196

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed
 

‎core/src/main/ruby/jruby/kernel.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module JRuby
2626
load 'jruby/kernel/enumerable.rb'
2727
load 'jruby/kernel/io.rb'
2828
load 'jruby/kernel/time.rb'
29-
autoload :GC, 'jruby/kernel/gc.rb'
29+
load 'jruby/kernel/gc.rb'
3030
load 'jruby/kernel/range.rb'
3131
load 'jruby/kernel/file.rb'
3232
load 'jruby/kernel/basicobject.rb'

‎core/src/main/ruby/jruby/kernel/gc.rb

+31-24
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ def self.stat(all_stats_or_key = {})
7070
module Profiler
7171
def self.__setup__
7272
@profiler ||= begin
73-
java.lang.Class.forName('com.sun.management.GarbageCollectionNotificationInfo')
74-
75-
# class exists, proceed with GC notification version
76-
@profiler = NotifiedProfiler.new
77-
rescue java.lang.ClassNotFoundException
78-
@profiler = NormalProfiler.new
73+
NotifiedProfiler.new!
74+
# class exists and Java dependecies are also present, proceed with GC notification version
75+
rescue NameError
76+
NormalProfiler.new
7977
end
8078
end
8179

@@ -115,35 +113,42 @@ def self.total_time
115113
end
116114

117115
begin
118-
javax.management.NotificationListener # try to access
119116
class NotifiedProfiler
120117
HEADER = " ID Type Timestamp(sec) Before(kB) After(kB) Delta(kB) Heap(kB) GC Time(ms) \n"
121118
FORMAT = "%5d %-20s %19.4f %13i %13i %12i %15i %20.10f\n"
122119

123-
class GCListener
124-
include javax.management.NotificationListener
125-
126-
def initialize
127-
@lines = []
128-
end
129-
130-
attr_accessor :lines
131-
132-
def handleNotification(notification, o)
133-
lines << notification
134-
end
135-
136-
def clear
137-
lines.clear
138-
end
120+
def self.new!
121+
javax.management.NotificationListener # try to access
122+
com.sun.management.GarbageCollectionNotificationInfo
123+
new
139124
end
140125

141126
def enabled?
142127
@gc_listener != nil
143128
end
144129

145130
def enable
146-
@gc_listener ||= GCListener.new
131+
@gc_listener ||= begin
132+
# delay JI class-loading, generate listener on first use :
133+
gc_listener = Class.new do # GCListener
134+
include javax.management.NotificationListener
135+
136+
def initialize
137+
@lines = []
138+
end
139+
140+
attr_accessor :lines
141+
142+
def handleNotification(notification, o)
143+
lines << notification
144+
end
145+
146+
def clear
147+
lines.clear
148+
end
149+
end
150+
gc_listener.new
151+
end
147152
java.lang.management.ManagementFactory.garbage_collector_mx_beans.each do |gc_bean|
148153
gc_bean.add_notification_listener @gc_listener, nil, nil
149154
end
@@ -233,6 +238,7 @@ def total_time
233238
return duration / 1000.0
234239
end
235240
end
241+
private_constant :NotifiedProfiler
236242
rescue Exception
237243
# ignore, leave it undefined
238244
end
@@ -278,6 +284,7 @@ def total_time
278284
(time - @start_time) / 1000.0
279285
end
280286
end
287+
private_constant :NormalProfiler
281288
end
282289
end
283290
end

0 commit comments

Comments
 (0)