Community
    • Login

    PythonScript plugin - variables and memory

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    pythonscript
    4 Posts 3 Posters 1.8k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Mikhail VM
      Mikhail V
      last edited by

      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.

      Claudia FrankC 1 Reply Last reply Reply Quote 1
      • Claudia FrankC
        Claudia Frank @Mikhail V
        last edited by

        @Mikhail-V

        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 like

        T = ''
        def main()
            global T
            T = editor.getText()
        main()
        

        then memory is stil used.

        Cheers
        Claudia

        1 Reply Last reply Reply Quote 3
        • Scott SumnerS
          Scott Sumner
          last edited by

          @Mikhail-V

          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 a T 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 a main in memory, available at the PS console’s >>> prompt, as well as to other scripts. Note that in this case there will be no T available, unless you ran the first script before this one. Because T is local to the main function, it has no effect on the non-local T (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.

          1 Reply Last reply Reply Quote 3
          • Scott SumnerS
            Scott Sumner
            last edited by

            @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 new T.

            1 Reply Last reply Reply Quote 2
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors