1

Python will keep the space alloced for int value even though the int value has been delete, showing as follows:

age = 9999
del age

the memory stored variable age will be used to store other int value, as a result, the free function will never be called for this memory, but I don't know does string object use the same strategy to manage memory? I mean the memory alloc for string will never be freed.

for why python will never free memory for int object, please read this blog: http://www.laurentluce.com/posts/python-integer-objects-implementation/

python will manage a free_list(python-2.7.3 source code:intobject.c line:45) to hold every memory alloc for a int object and will never dealloc it

2
  • 1
    Can you explain why you think the memory won't be freed? Commented Jan 27, 2013 at 15:48
  • CPython does cache some low integer values (as they are immutable). This is, however, an implementation detail. Commented Jan 27, 2013 at 16:01

2 Answers 2

5

Python manages all memory for you. If a value is referenced by a name, it will remain in memory. Once a value is no longer referenced by any names, the interpreter is free to reclaim the memory. You don't have to worry about this.

I don't understand your example, or why you think the memory used by "age" will be used over again. Keep in mind, Python names are not like C variables: they don't represent allocated memory. They are labels that can refer to values.

Sign up to request clarification or add additional context in comments.

Comments

2

If you take a look at the python string implementation you'll find the releavent line:

op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);

Which shows that strings are simply allocated using python's malloc. They don't use pools like integers because python strings objects vary in size depending on the length of the string. So you couldn't use a pool in the same way you do for integers.

See: http://www6.uniovi.es/python/dev/src/c/html/stringobject_8c-source.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.