@@ -177,6 +177,19 @@ private boolean isFull() {
177
177
return counter == service .cacheSize ;
178
178
}
179
179
180
+ /**
181
+ * This method is called when finalizing the {@link MarkerThreadLocalData} of another
182
+ * thread. we want to either queue the running of mark functions for any objects still kept
183
+ * alive by that other thread, or copy them into our kept objects. If our keptObjects buffer
184
+ * is already full we should queue and reset it first, and then consider one of four cases.
185
+ *
186
+ * 1. The other threads kept object buffer is full. We'll just queue that.
187
+ * 2. The other thread has an empty kept object buffer. We don't need to do anything.
188
+ * 3. The whole of the other thread's kept object buffer will fit into our buffer.
189
+ * We can just copy its contents into our buffer.
190
+ * 4. The other buffer will not fit into our buffer. We need to copy across as much as will
191
+ * fit, queue the now full buffer, and then copy the rest.
192
+ */
180
193
public void keepObjects (MarkerKeptObjects otherObjects ) {
181
194
if (isFull ()) {
182
195
queueAndReset ();
@@ -245,6 +258,13 @@ public MarkingService(RubyContext context, ReferenceProcessor referenceProcessor
245
258
public MarkerThreadLocalData makeThreadLocalData () {
246
259
MarkerThreadLocalData data = new MarkerThreadLocalData (this );
247
260
MarkerKeptObjects keptObjects = data .getKeptObjects ();
261
+ /*
262
+ * This finalizer will ensure all the objects remaining in our kept objects buffer will be
263
+ * queue at some point. We don't need to do a queue and reset as the MarkerKeptObjects is
264
+ * going to be GC'ed anyway. We also don't simply queue the keptObjects buffer because it
265
+ * may only have zero, or very few, objects in it and we don't want to run mark functions
266
+ * more often than we have to.
267
+ */
248
268
context .getFinalizationService ().addFinalizer (data , null , MarkingService .class , () -> getThreadLocalData ().keptObjects .keepObjects (keptObjects ), null );
249
269
return data ;
250
270
}
0 commit comments