when does python delete variables?

前端 未结 4 1592
借酒劲吻你
借酒劲吻你 2020-12-14 22:15

I know that python has an automatic garbage collector and so it should automatically delete variables when there are no more reference to them.

My impression is tha

4条回答
  •  悲&欢浪女
    2020-12-14 23:05

    It depends on the implementation and the type of variable. For simple objects like ints there are some optimisations. In CPython, for example, a simple int will reuse the same memory, even after del has been used. You can't count on that, but it does illustrate that things are more complex than they appear.

    Remember that when you del you are deleting a name, not necessarily an object.
    For example:

    # x is a np.array and contains a lot of data
    

    Would be more accurately worded as:

    # x references a np.array which contains a lot of data
    

    del will decrement the reference count on that object, but even when it drops to zero it is not guaranteed to be garbage collected any time soon.

    Suggest you look at the gc module for an explanation and inspiration. Then think again.

    If you are getting "out of memory" then you probably have a fundamental problem with your design. Most likely you are loading too much data in at one go (try using iterators?), or maybe your code need to be structured better.

    I just saw your edit. Do you need all of that array in memory at the same time? Could you use a generator?

    Another alternative is to use a database like SQLite or maybe a shelve

提交回复
热议问题