Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory use of a script
#1
Hi,

I’ve two files: main and slave. The main imports the slave and executes a function in it.

The question is, does the slave script remain in the memory once it returns a value requested by the main file? If so, how do I exit from the file unloading it correctly from the memories?

TIA
Reply
#2
Short answer: you shouldn't worry about it unless you're actively having issues. If you are actively having issues, I strongly recommend you refactor your code to use object oriented programming where a cache can be managed explicitly rather than what I'm about to say to answer your question.

In your code you have import slave. Once you're done with it, you can do del slave and then Python, when it feels like it, can free the memory associated. You can look into the garbage collection module if that's still not enough.
Reply
#3
The issue is that a function in slave is called every minute. After few hours, I may be left with no memory space if the slave file doesn't close correctly. Will look into garbage collector. Thx
Reply
#4
The function call should go on the stack, which should be cleaned up after the function call exits. What state do you have that is causing this problem? From what you've said so far, garbage collector is probably not the way to go.
Reply
#5
No problems, I'm in the desigh stage and I was just wondering how the files close up and release memory space.
Reply
#6
If your function is dealing with files, it should be closing them explicitly. A with-block (or "context manager") is generally the normal way to do that.

Even if you weren't doing that, your file objects would (eventually) be closed by the garbage collector, with or without hitting the whole module, since the files are kept on the stack which is cleaned up after the function call completes.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020