I have certain code that uses many instances of a SoftReference
subclass. I would like test that it works correctly in cases when all / only some / none of these references are staged for clearing in ReferenceQueue
. For the case of "none" this is quite easy: create strong references to the objects, and the soft references are guaranteed to stay. However, how would I go about guaranteeing them to be cleared? As I understand, System.gc()
is only a request to run garbage collector, and even if it actually runs, it may well decide to not collect all unreachable objects...
Also the code is quite performance critical, so it's not a good idea to alter it just for testing purposes. (Adding a test-only method that doesn't affect other methods is fine, but adding paths that are used only for testing in other methods is something to avoid).
Aucun commentaire:
Enregistrer un commentaire