1

In Python, if I have a variable name and reset it with an object, could that eventually fill memory?

while True:
    a = 'test string'
2
  • 1
    Does this answer your question? What happens to memory locations in Python when you overwrite a variable? Commented May 26, 2020 at 19:53
  • @AnnZen although in this case it seems that (at least in the implementation I'm using) Python will "intern" the string constant 'test string', so that it keeps reusing the same object every time - this is evidenced by adding 'print(id(a))' after the assignment. Commented May 26, 2020 at 20:31

1 Answer 1

2

Python is allocating memory for objects that created when referencing new variables if they not exist.
In your case, the memory allocation will take place once on the first iteration, and then it will keep referencing the same object in each iteration so it will not allocate the full memory that you have.
This loop can run until you will kill the process without warring about memory space.

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

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.