Does Python garbage collect variables that are no longer referenced while within the scope of a function?
While waiting for a long running function to finish executing, I began thinking about whether the garbage collector will clean up references to variables which will no longer be used.
Say for example, I have a function like:
def long_running_function():
x = MemoryIntensiveObject()
print id(x)
# lots of hard work done here which does not reference x
return
I'm intrigued whether the interpreter is smart enough to realize that x is no longer used and can be dereferenced. It's somewhat hard to test, as I can write code to check its reference count, but that implicitly then references it which obviates the reason for doing it.
My thought is, perhaps when the function is parsed and the bytecode is generated, it may be generated in such a way that will allow it to clean up the object when it can no longer be referenced.
Or, is the answer just simpler that, as long as we're still within a scope where it "could" be used, it won't be cleaned up?
python garbage-collection cpython
add a comment |
While waiting for a long running function to finish executing, I began thinking about whether the garbage collector will clean up references to variables which will no longer be used.
Say for example, I have a function like:
def long_running_function():
x = MemoryIntensiveObject()
print id(x)
# lots of hard work done here which does not reference x
return
I'm intrigued whether the interpreter is smart enough to realize that x is no longer used and can be dereferenced. It's somewhat hard to test, as I can write code to check its reference count, but that implicitly then references it which obviates the reason for doing it.
My thought is, perhaps when the function is parsed and the bytecode is generated, it may be generated in such a way that will allow it to clean up the object when it can no longer be referenced.
Or, is the answer just simpler that, as long as we're still within a scope where it "could" be used, it won't be cleaned up?
python garbage-collection cpython
2
Are you asking about CPython specifically, or some other Python implementation (e.g. Jython or PyPy)? The answers will likely be different.
– Daniel Pryden
Dec 27 '18 at 18:23
CPython is the version I'm most interested in, though thoughts on alternative implementations would be useful! (I updated the tag accordingly)
– David
Dec 27 '18 at 18:25
add a comment |
While waiting for a long running function to finish executing, I began thinking about whether the garbage collector will clean up references to variables which will no longer be used.
Say for example, I have a function like:
def long_running_function():
x = MemoryIntensiveObject()
print id(x)
# lots of hard work done here which does not reference x
return
I'm intrigued whether the interpreter is smart enough to realize that x is no longer used and can be dereferenced. It's somewhat hard to test, as I can write code to check its reference count, but that implicitly then references it which obviates the reason for doing it.
My thought is, perhaps when the function is parsed and the bytecode is generated, it may be generated in such a way that will allow it to clean up the object when it can no longer be referenced.
Or, is the answer just simpler that, as long as we're still within a scope where it "could" be used, it won't be cleaned up?
python garbage-collection cpython
While waiting for a long running function to finish executing, I began thinking about whether the garbage collector will clean up references to variables which will no longer be used.
Say for example, I have a function like:
def long_running_function():
x = MemoryIntensiveObject()
print id(x)
# lots of hard work done here which does not reference x
return
I'm intrigued whether the interpreter is smart enough to realize that x is no longer used and can be dereferenced. It's somewhat hard to test, as I can write code to check its reference count, but that implicitly then references it which obviates the reason for doing it.
My thought is, perhaps when the function is parsed and the bytecode is generated, it may be generated in such a way that will allow it to clean up the object when it can no longer be referenced.
Or, is the answer just simpler that, as long as we're still within a scope where it "could" be used, it won't be cleaned up?
python garbage-collection cpython
python garbage-collection cpython
edited Dec 27 '18 at 18:25
asked Dec 27 '18 at 18:22
David
224
224
2
Are you asking about CPython specifically, or some other Python implementation (e.g. Jython or PyPy)? The answers will likely be different.
– Daniel Pryden
Dec 27 '18 at 18:23
CPython is the version I'm most interested in, though thoughts on alternative implementations would be useful! (I updated the tag accordingly)
– David
Dec 27 '18 at 18:25
add a comment |
2
Are you asking about CPython specifically, or some other Python implementation (e.g. Jython or PyPy)? The answers will likely be different.
– Daniel Pryden
Dec 27 '18 at 18:23
CPython is the version I'm most interested in, though thoughts on alternative implementations would be useful! (I updated the tag accordingly)
– David
Dec 27 '18 at 18:25
2
2
Are you asking about CPython specifically, or some other Python implementation (e.g. Jython or PyPy)? The answers will likely be different.
– Daniel Pryden
Dec 27 '18 at 18:23
Are you asking about CPython specifically, or some other Python implementation (e.g. Jython or PyPy)? The answers will likely be different.
– Daniel Pryden
Dec 27 '18 at 18:23
CPython is the version I'm most interested in, though thoughts on alternative implementations would be useful! (I updated the tag accordingly)
– David
Dec 27 '18 at 18:25
CPython is the version I'm most interested in, though thoughts on alternative implementations would be useful! (I updated the tag accordingly)
– David
Dec 27 '18 at 18:25
add a comment |
1 Answer
1
active
oldest
votes
No, CPython will not garbage collect an object as long as a name that references that object is still defined in the current scope.
This is because, even if there are no references to the name x
as literals in the code, calls to vars()
or locals()
could still grab a copy of the locals namespace dictionary (either before or after the last reference to x
) and therefore the entire locals namespace effectively "roots" the values it references until execution leaves its scope.
I don't know for certain how other implementations do this. In particular, in a JIT-compiled implementation like PyPy, Jython, or IronPython, it is possible at least in theory for this optimization to be performed. The JVM and CLR JITs actually do perform this optimization in practice on other languages. Whether Python on those platforms would be able to take advantage or not depends entirely on the bytecode that the Python code gets compiled into.
I suspected that locals() might play a part, but you've given me food for thought on how the JIT-compiled versions work and the scope for additional optimizations. Thank you!
– David
Dec 27 '18 at 19:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949272%2fdoes-python-garbage-collect-variables-that-are-no-longer-referenced-while-within%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, CPython will not garbage collect an object as long as a name that references that object is still defined in the current scope.
This is because, even if there are no references to the name x
as literals in the code, calls to vars()
or locals()
could still grab a copy of the locals namespace dictionary (either before or after the last reference to x
) and therefore the entire locals namespace effectively "roots" the values it references until execution leaves its scope.
I don't know for certain how other implementations do this. In particular, in a JIT-compiled implementation like PyPy, Jython, or IronPython, it is possible at least in theory for this optimization to be performed. The JVM and CLR JITs actually do perform this optimization in practice on other languages. Whether Python on those platforms would be able to take advantage or not depends entirely on the bytecode that the Python code gets compiled into.
I suspected that locals() might play a part, but you've given me food for thought on how the JIT-compiled versions work and the scope for additional optimizations. Thank you!
– David
Dec 27 '18 at 19:24
add a comment |
No, CPython will not garbage collect an object as long as a name that references that object is still defined in the current scope.
This is because, even if there are no references to the name x
as literals in the code, calls to vars()
or locals()
could still grab a copy of the locals namespace dictionary (either before or after the last reference to x
) and therefore the entire locals namespace effectively "roots" the values it references until execution leaves its scope.
I don't know for certain how other implementations do this. In particular, in a JIT-compiled implementation like PyPy, Jython, or IronPython, it is possible at least in theory for this optimization to be performed. The JVM and CLR JITs actually do perform this optimization in practice on other languages. Whether Python on those platforms would be able to take advantage or not depends entirely on the bytecode that the Python code gets compiled into.
I suspected that locals() might play a part, but you've given me food for thought on how the JIT-compiled versions work and the scope for additional optimizations. Thank you!
– David
Dec 27 '18 at 19:24
add a comment |
No, CPython will not garbage collect an object as long as a name that references that object is still defined in the current scope.
This is because, even if there are no references to the name x
as literals in the code, calls to vars()
or locals()
could still grab a copy of the locals namespace dictionary (either before or after the last reference to x
) and therefore the entire locals namespace effectively "roots" the values it references until execution leaves its scope.
I don't know for certain how other implementations do this. In particular, in a JIT-compiled implementation like PyPy, Jython, or IronPython, it is possible at least in theory for this optimization to be performed. The JVM and CLR JITs actually do perform this optimization in practice on other languages. Whether Python on those platforms would be able to take advantage or not depends entirely on the bytecode that the Python code gets compiled into.
No, CPython will not garbage collect an object as long as a name that references that object is still defined in the current scope.
This is because, even if there are no references to the name x
as literals in the code, calls to vars()
or locals()
could still grab a copy of the locals namespace dictionary (either before or after the last reference to x
) and therefore the entire locals namespace effectively "roots" the values it references until execution leaves its scope.
I don't know for certain how other implementations do this. In particular, in a JIT-compiled implementation like PyPy, Jython, or IronPython, it is possible at least in theory for this optimization to be performed. The JVM and CLR JITs actually do perform this optimization in practice on other languages. Whether Python on those platforms would be able to take advantage or not depends entirely on the bytecode that the Python code gets compiled into.
answered Dec 27 '18 at 18:30
Daniel Pryden
45.2k872117
45.2k872117
I suspected that locals() might play a part, but you've given me food for thought on how the JIT-compiled versions work and the scope for additional optimizations. Thank you!
– David
Dec 27 '18 at 19:24
add a comment |
I suspected that locals() might play a part, but you've given me food for thought on how the JIT-compiled versions work and the scope for additional optimizations. Thank you!
– David
Dec 27 '18 at 19:24
I suspected that locals() might play a part, but you've given me food for thought on how the JIT-compiled versions work and the scope for additional optimizations. Thank you!
– David
Dec 27 '18 at 19:24
I suspected that locals() might play a part, but you've given me food for thought on how the JIT-compiled versions work and the scope for additional optimizations. Thank you!
– David
Dec 27 '18 at 19:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949272%2fdoes-python-garbage-collect-variables-that-are-no-longer-referenced-while-within%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Are you asking about CPython specifically, or some other Python implementation (e.g. Jython or PyPy)? The answers will likely be different.
– Daniel Pryden
Dec 27 '18 at 18:23
CPython is the version I'm most interested in, though thoughts on alternative implementations would be useful! (I updated the tag accordingly)
– David
Dec 27 '18 at 18:25