Saturday 16 February 2019

Howto delete all object pointer in java

Not explicitly, no. And there's a good reason for that - it's just not the way garbage collection works in Java!



If you really want to do this (though I'm saying this mostly for academic purposes, I'd strongly advise not doing it) the best approach I can think of is potentially wrapping up all references to the object in WeakReferences, and pass those around, just holding onto one strong reference to your object. If you set that strong reference to null then all the weak references to that object become eligible for garbage collection.



Of course, you need to be aware that as soon as the hard reference is set to null, the WeakReferences could disappear randomly at any time afterwards. So you need to make sure all your code using them is ready to handle that.



That said, that route is almost not the best to go down, and will make your code a lot harder to maintain! Ensuring that they're all released as they go out of scope nicely would be by far the better alternative, and shouldn't be hard if the rest of your code is neat enough.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...