When memory issues are critical, do I save some memory when I do my python imports inside a function, so as to when the call finishes everything will be discarder from memory? Or this cumbers more my memory and CPU especially when I do a lot of calls of the specific function? (The user does the calls and I do not know in advance how many she will do). What are the memory implications of this difference?
Asked
Active
Viewed 681 times
0
-
I do not think this is a duplicate. The question you refer to sees the problem from a perception of clarity but not of memory issues. I am asking about what is better for the memory – pebox11 Sep 02 '15 at 13:02
-
Read the answers on the other question also. They contain detailed explanation regarding *perfomance* related issues. Please do go through the other answers also. – Bhargav Rao Sep 02 '15 at 13:05
-
Went through the suggested question but only mentions something about performance but does not mention anything about memory. And performance is not always related to memory. Can you point to me where your suggested question mentions memory issues? If you do I can hapilly discard this question. – pebox11 Sep 02 '15 at 13:08
-
I have reopened the question and let the community to decide. [Linking the question here](http://stackoverflow.com/questions/1024049/is-it-pythonic-to-import-inside-functions) – Bhargav Rao Sep 02 '15 at 13:14
-
Thanks for removing [duplicate] – pebox11 Sep 02 '15 at 13:14
-
3why don't you do some profiling of your code and find out? – taesu Sep 02 '15 at 13:15
-
4On the issue of RAM usage, [this answer](http://stackoverflow.com/a/3095124/4014959) (which is linked from a comment in Bhargav's link) mentions that imports are _always_ cached, even when performed in a function, so the memory isn't released when the function is exited. – PM 2Ring Sep 02 '15 at 13:17
-
2If I am reading [the module cache](https://docs.python.org/3/reference/import.html#the-module-cache) properly, I don't think modules get garbage collected the same way regular variables do at the end of a function. Perhaps deleting entries from `sys.modules` will accomplish what you want, but I've never tried it. – Kevin Sep 02 '15 at 13:18
-
`@PM 2Ring` : Thank you very helpful link! – pebox11 Sep 02 '15 at 13:25
-
@Kevin: I doubt that will work. Other modules may hold references to the module if they imported it. Deleting it from `sys.modules` and re-importing means there are now two different instances of the same module floating around, which is... undesirable. (That's a different Kevin; I'm *not* talking to myself!) – Kevin Sep 02 '15 at 14:11
1 Answers
2
When you import a module, any/all module objects/functions/etc are cached so that importing the same module again is a no-op. Subsequently these objects/functions/etc will not be freed when the local names referring to them go out of scope. This only affects functions and objects defined globally within the module and it's very likely that there won't be a lot of those, so it's probably not something to worry about.
To specifically answer your question, there is effectively no difference in terms of performance unless the import is inside a function or branch which is never executed. In that rare case, having it inside the branch or function is slightly faster/less resource intensive, but it won't gain you much.

Chad S.
- 6,252
- 15
- 25