How to force finalization (for testing) in Cuis/Squeak/Pharo?

214 views Asked by At

I've implemented a few ExternalStrctures (as part of an "FFI effort"), and for some of them I want to implement finalization for reclaiming the external memory.

I'm trying to write some tests for that, and it seems no matter how many times I force a garbage collection with Smalltalk garbageCollect, finalization is (apparently) never triggered.

To be sure of that I placed an external breakpoint (using gdb) in the function I'm calling from the finalizer, but the breakpoint is never hit. I've also inspected all references to the object (after running many GCs) and the only reference is the WeakRegistry (I got to the object using allInstances).

Is there a way to force finalization (in Cuis/Pharo/Squeak)?

2

There are 2 answers

1
codefrau On BEST ANSWER

Smalltalk garbageCollect should always trigger finalization. If the object is new, then Smalltalk garbageCollectMost might trigger it, too (depending on the VM).

Note that finalization is implemented by a process in the image. If that process is not running for some reason, no finalization will happen.

Also, there are historically different ways for the VM to signal the image which objects to finalize. Initially, the finalization process would have to scan all weak objects, which was robust, but inefficient. Nowadays the VM and image will have to agree on a method to find the objects to finalize. If these are mismatched, finalization might not be reliable.

2
Juan Vuletich On

I think that #testFinalization in the OpenCL.pck.st package could be of help, at least as a working experiment to start with.