PythonScript plugin - variables and memory
-
IIUC all globals of any script remain in memory, even when the script is ended.
Different scripts have one common global namespace?
So those are removed only after NPP restart.So, basically this means no auto memory cleaning?
But does this happens only in this situation - i.e. for unique global names, or does new
memory allocate constantly also for same global names?So I am curious, how does Pythonscript plugin manages memory?
e.g. such script:T = editor.getText()
or this script:
def main() T = editor.getText() main()
What happens in memory in each case?
I mean if I run the script over and over again. -
So I am curious, how does Pythonscript plugin manages memory?
basically it is doing the same as an interactive python shell session.
If the reference count of an variable is >0 then it is kept alive otherwise
memory released.Memory allocated by a variable inside a function does get release as soon as the
function ends as long as the variable is only used in local function scope.So, in this case,
def main() T = editor.getText() main()
memory used by T is released once main function ends
but if you would have used something likeT = '' def main() global T T = editor.getText() main()
then memory is stil used.
Cheers
Claudia -
What happens in memory in each case? I mean if I run the script over and over again.
For this case:
T = editor.getText()
Running repeatedly will cause the previous memory held by
T
(if any) to be overwritten with new contents. After the final invocation of the script there will still be aT
in memory, available at the PS console’s>>>
prompt, as well as to other scripts.And for this case:
def main() T = editor.getText() main()
Similarly, running repeatedly will cause the previous memory held by
main
(if any) to be overwritten with new contents. After the final invocation of the script there will still be amain
in memory, available at the PS console’s>>>
prompt, as well as to other scripts. Note that in this case there will be noT
available, unless you ran the first script before this one. BecauseT
is local to themain
function, it has no effect on the non-localT
(from earlier).Personal note:
What I do here is to name any variables outside of function scope with a (hopefully) unique prefix. I have done this ever since I created a bug (and a debugging nightmare for myself) when I used the same non-local variable name in two different scripts.
For example if my script is named
ThisIsMyCoolPythonScript.py
I take the first letter of each word (TIMCPS
), add a double-underscore (TIMCPS__
) and then use this as a prefix on names that will continue to live once the pythonscript ends.So I might take your / @Claudia-Frank 's code snippets and turn them into:
TIMCPS__def main() T = editor.getText() TIMCPS__main()
…and… :
TIMCPS__T = '' def TIMCPS__main() global TIMCPS__T TIMCPS__T = editor.getText() TIMCPS__main()
And really a side note, but related:
When I need to test to see if a script has been run previously, I do something like this:
try: TIMCPS__main except NameError: pass # has NOT been run previously else: pass # has been run previously
Hope this helps.
-
@Scott-Sumner said:
previous memory held by T (if any) to be overwritten with new contents
What I meant here I could have said better…“overwritten” and “new contents” may be rather vague…let me try again:
Running repeatedly will cause the memory occupied by a previous
T
(if any) to be released, and memory to be allocated to hold the newT
.