-1

Easy question, is it possible to remove an object from memory and setting all remaining pointers to it to undefined?

user2741831
  • 2,120
  • 2
  • 22
  • 43
  • 1
    Do you mean forcibly dereference it from all of the identifiers/containers currently holding a reference to it? Could you expand on the context of the problem you're actually trying to solve? – jonrsharpe Jun 28 '18 at 21:11
  • 1
    you mean this ( https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary ) – kadalamittai Jun 28 '18 at 21:12
  • [This](https://stackoverflow.com/q/3013304/1586200) question may help if that's what you mean. – Autonomous Jun 28 '18 at 21:13
  • In case someone tries to recommend `pyjack.replace_all_refs`: no, that can't actually get all the references, it just tries its best, the name is misleading. – user2357112 Jun 28 '18 at 21:13
  • Possible duplicate of [How can I explicitly free memory in Python?](https://stackoverflow.com/questions/1316767/how-can-i-explicitly-free-memory-in-python) – MoxieBall Jun 28 '18 at 21:20
  • @MoxieBall nope. That simply runs the supplemental garbage collector, which only handles unreachable reference cycles. – juanpa.arrivillaga Jun 28 '18 at 21:38
  • @juanpa.arrivillaga I agree that the accepted answer doesn't answer this question but many of the rest do. Perhaps that wasn't enough of a reason to mark this as a dup – MoxieBall Jun 28 '18 at 21:44
  • Possible duplicate of [How to delete every reference of an object in Python?](https://stackoverflow.com/questions/3013304/how-to-delete-every-reference-of-an-object-in-python) – Jacob Jun 28 '18 at 23:13

2 Answers2

2

You cannot explicitly free memory in Python.

If you want to call del x without having other references to x preventing it from getting garbage collected, you may want to check out weakrefs.

MoxieBall
  • 1,907
  • 7
  • 23
0

In case you are looking to create some sort of 'cancelable' object that can be invalidated and cause all subsequent attempts to use it to get either a None value or an error, you can do that (after a fashion), but your code will have to be disciplined not to get a direct reference ever, but always refer to the object in a special way, for example:

class deletable(object):
    def __init__(self, value):
        self.v = value
    def destroy(self):
        if hasattr(self,"v"): delattr(self, "v")

# create instance
x = deletable( { "key" : "value", "other" : 13 } )

# access
print (x.v["key"])

# re-assign
x.v = { "another" : "dict" }

# "destroy" - option 1 - trigger error on use
x.destroy()

# "destroy" - option 2 - make it None (free memory, but keep x.v valid)
x.v = None # or x.v = {}, if you want to keep it with the original data type of dict()

Now, this "works" on the condition that you never (EVER) do z = x.v and always pass around x as a whole.

(If you know in advance the data type and it is always the same, e.g., dict, you can do a bit better and have the custom object respond to things like x["key"], x.update(some_dict), etc., i.e., look like a regular dict, but still able to call x.destroy() and make it fail further attempts to access. Advanced stuff, I won't write a whole article here on this, not even knowing that I made a correct guess as to what you really need).

Leo K
  • 5,189
  • 3
  • 12
  • 27