-1

Say i store a password in plain text in a variable called passWd as a string. How does python release this variable once i discard of it (for instance, with del passWd or passWd= 'new random data')?

Is the string stored as a byte-array meaning it can be overwritten in the memoryplace that it originally existed or is it a fixed set in a memory area which can't be modified and there for when assining a new value a new memory area is created and the old area is discareded but not overwritten by null?

I'm questioning how Python implements the safety of memory areas and would like to know more about it, mainly because i'm curious :)

From what i've gathered so far, using del (or __del__) causes the interpreter to not release memory areas of that variable automaticly which can cause issues, and also i'm not sure that del is so thurrow on deleting the values. But that's just from what i've gathered and not something in black or white :)

6
  • While many people may know the answer here on SU, this should be posted on Stack Overflow, I assume it'll be migrated soon.
    – nerdwaller
    Apr 9, 2013 at 14:36
  • I didn't dare to ask on SO, since my question isn't "i need help with this code problem" it's more of, "how is the memory management implemented on lower levels" :) but i guess i can give SO a chance to not hate on me :P
    – Torxed
    Apr 9, 2013 at 14:38
  • 1
    Just a suggestion for posting over there: use a valid identifier since pass is a keyword in Python and couldn't be used :) Most people over there are pretty reasonable.
    – nerdwaller
    Apr 9, 2013 at 14:41
  • A good and a valid point! Thank you! :) Also, you are very reasonable!
    – Torxed
    Apr 9, 2013 at 14:42
  • Next time please don't cross post. As @nerdwaller said, your question could have been migrated.
    – slhck
    Apr 9, 2013 at 15:32

1 Answer 1

1

the answer is implementation specific. from http://docs.python.org/2/library/stdtypes.html

under 5.6 Sequence Types, take note of "Notes #6" which explains that some implementations (CPython for one) do in-place string updates in memory, whereas others presumably treat the string as a immutable object, that when replaced/altered defines a new area in memory.

2
  • I was hoping that was the case, otherwise i would have needed to write some C code of my own to actually do that job, if python didn't have a workaround for it that is. Assuming Py2.6+ there are some neat stuff like in-place concationations occur which is great.. But how about deletions, are they handled in a way that the momory is actually cleared, replaced by null or is it a standard free() on a malloc allocation? According to StackOverflow it's just freed, making it a security risk and i would have to do passWd = '...' before freeing the memory area, and even then, can i be sure?
    – Torxed
    Apr 9, 2013 at 14:53
  • python is an intrepreter langague and uses garbage collection as the default memory recovery mechanism. per docs.python.org/2/library/gc.html objects that do not define __del__() are garbage collected, whereas objects that do are not collectable and appear in the gc.Garbage return and must be released manually. running gc.collect() will cause the system to try to start disposing unlinked objects in memory, but garbage collection is never an explicit thing, but a best effort attempt. Apr 9, 2013 at 15:17

Not the answer you're looking for? Browse other questions tagged .