I would like to migrate our code away from Finalizers and use PhantomReference
objects instead to take care of our problem of deleting native data associated
with GC'd Java objects. However, when I converted some of our code to use
Phantom (or Weak) references, I discovered that the int[] objects I was creating
(and then reference in native code via GlobalWeakRef) were only sometimes
getting GC'd.
Since these buffers consume up to several hundred K per object (some apps
allocate new back buffers per frame, which ends up creating one of these
kinds of arrays per frame), this leak soon causes an app to run out of
memory in the java heap and crash.
I have attached a self-contained example of the problem. The class
hierarchy in the example mimics the hierarchy of classes in our application,
just as it mimics the problems we are seeing with memory leaks.
To run the example, type:
java NativeArrayReferencesTest
The application will print out messages when objects are GC'd and will
eventually print an OutOfMemoryError.
objects instead to take care of our problem of deleting native data associated
with GC'd Java objects. However, when I converted some of our code to use
Phantom (or Weak) references, I discovered that the int[] objects I was creating
(and then reference in native code via GlobalWeakRef) were only sometimes
getting GC'd.
Since these buffers consume up to several hundred K per object (some apps
allocate new back buffers per frame, which ends up creating one of these
kinds of arrays per frame), this leak soon causes an app to run out of
memory in the java heap and crash.
I have attached a self-contained example of the problem. The class
hierarchy in the example mimics the hierarchy of classes in our application,
just as it mimics the problems we are seeing with memory leaks.
To run the example, type:
java NativeArrayReferencesTest
The application will print out messages when objects are GC'd and will
eventually print an OutOfMemoryError.