You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider the following program, which allocates large arrays and stores them in weak references. It terminates when it detects the that a weak reference has been GCed.
importjava.lang.ref.WeakReferenceclassMyWeakRef[A](a: A) extendsWeakReference(a)
@main defmain=valrefs=newArray[MyWeakRef[Array[Long]]](Int.MaxValue/8)
vari=0while (i < refs.length) {
refs(i) =newMyWeakRef(newArray[Long](Int.MaxValue/64))
varj=0while (j < i) {
if (refs(j).get ==null) {
println((i, j))
System.exit(0)
}
j +=1
}
i +=1
}
As written, that program never prints or terminates for me. This suggests that none of the arrays are being GCed.
However, if I replace MyWeakRef with WeakReference then it terminates quickly with (3,0). If I run the program on the JVM, it also terminates quickly.
Motivation
In Cats Effect we sub-class WeakReference for a custom data structure. We use this data structure to power a "fiber dump" feature (similar to thread dump).
For the WeakReference detection we can use simplified algorithm, because WeakReference is a class, and trait would never be instantiated, we might get:
It seems that if
WeakReference
is sub-classed then it is not correctly handled by the GC. Probably due to the logic here?scala-native/nativelib/src/main/resources/scala-native/gc/immix_commix/headers/ObjectHeader.h
Lines 88 to 90 in e5ee79a
Consider the following program, which allocates large arrays and stores them in weak references. It terminates when it detects the that a weak reference has been GCed.
As written, that program never prints or terminates for me. This suggests that none of the arrays are being GCed.
However, if I replace
MyWeakRef
withWeakReference
then it terminates quickly with(3,0)
. If I run the program on the JVM, it also terminates quickly.Motivation
In Cats Effect we sub-class
WeakReference
for a custom data structure. We use this data structure to power a "fiber dump" feature (similar to thread dump).https://github.com/typelevel/cats-effect/blob/e9aeb8ccb53016a37588256f4c8086f8ef1612bc/core/shared/src/main/scala/cats/effect/unsafe/WeakBag.scala#L108-L110
The text was updated successfully, but these errors were encountered: