Forcefully deallocating objects
suggest changeThis uses the ctypes module.
WARNING: doing this will leave your Python environment unstable and prone to crashing without a traceback! Using this method could also introduce security problems (quite unlikely). Only deallocate objects you’re sure you’ll never reference again. Ever.
import ctypes
deallocated = 12345
ctypes.pythonapi._Py_Dealloc(ctypes.py_object(deallocated))
import ctypes, sys
deallocated = 12345
(ctypes.c_char * sys.getsizeof(deallocated)).from_address(id(deallocated))[:4] = '\x00' * 4
The second assignment raises TypeError on Python 3: the buffer expects bytes, not the str '\x00' * 4. The _Py_Dealloc name is still present; that call was not run.
After a successful deallocation, any reference to the now deallocated object will cause Python to either produce undefined behavior or crash - without a traceback. There was probably a reason why the garbage collector didn’t remove that object…
If you deallocate None, you get a special message - Fatal Python error: deallocating None before crashing.