13

Edit: I have added an addendum to the question here that should reduce the scope of the question. Apologies to all that I have annoyed and wasted time by perhaps posting the question to soon, it wasn't my intention, and I have learned how better to do it next time.

I have a question that I'd like to ask, but I'm afraid that it may be closed as too broad. I'm wondering whether someone can check it through first and offer suggestions. Here it is:

Is it possible for a Python function to still use memory after being called?

If I run a function in Python 3 (func()) is there anything that could be inside func() that would cause it to increase its memory usage?

For instance, will running

def func(): 
    # Anything and everything in here

while True:
    func()

ever cause the program run out of memory, no matter what is in func()?

If the program is continually using memory, what are some possible things that could be going on in func() to cause it to continue using memory after it has been called?

17
  • 14
    I'm no Python guy, but I'm fairly certain that is going to be too broad. It sounds like you have a specific example that you're shying away from, you should start with that example and ask why it is behaving the way it is.
    – user4639281
    Commented Jun 6, 2017 at 20:50
  • 2
    I think your question is a yes/no question followed by explanation. i won't flag as too broad. anyway you shouldn't be afraid your question will get closed as too broad, if so then you just have to look for ways to re-factor you question to specific situation.
    – ytobi
    Commented Jun 6, 2017 at 20:58
  • 2
    Yes, I do have a specific example, but it is very large and complex, using other libraries that I have no confidence that they are not causing memory leaks. Commented Jun 6, 2017 at 21:00
  • 16
    Narrator voiceover it was.
    – jonrsharpe
    Commented Jun 6, 2017 at 21:14
  • 1
    If it truly is just a yes/no question, it likely isn't too broad, it's just... not useful. but it's more likely that any answerer to this question will assume you actually want examples of where it would consume more memory, which would be too broad.
    – Kevin B
    Commented Jun 6, 2017 at 21:15
  • 2
    It's the "anything" in "is there anything that could be inside func()" that's the trick. You could conceivably get 10,000 answers, all equally correct. Commented Jun 6, 2017 at 21:34
  • 2
    @MikeMcCaughan They're not asking for examples of things to put there. They're asking if there exists at least one snippet of code which will cause a memory leak. It's a yes/no answer.
    – Rob Mod
    Commented Jun 7, 2017 at 1:22
  • 3
    I think whether it is too broad depends on the answer. If the answer is "No", then that is the whole answer (not too broad). If the answer is "Yes, in a few specific ways", then probably still not too broad. Only if the answer is "Yes, there are tons of ways" then the question is too broad. Commented Jun 7, 2017 at 8:21
  • 4
    The real answer to your solution is to get a memory profiler that can track objects in memory. You can then observe what types are sticking around, which tells you where in your code the issue is. Not a py guy, no recommendations.
    – user1228
    Commented Jun 7, 2017 at 16:24
  • Add a list to func.__dict__ and append an item to it inside func. func then infinitely increases in size Commented Jun 7, 2017 at 23:24
  • 4
    I don't know about Python, but in other languages that second question of what can cause memory leaks or run away consumption, can be limited to just a few specific types of memory leaks. I could answer that question for C# or C++ in 3-5 bullet points. Of course I could expand that to 1000s if I started generating every permutation of a code snippet that generated a memory leak, but that'd be a silly way to answer such a question.
    – AaronLS
    Commented Jun 8, 2017 at 12:34
  • 1
    Why bother asking if the question is Too Broad when you're just going to ignore everyone telling you that it's Too Broad and ask the question anyway? You knew what you needed to change to fix the question, and just blatantly ignored that information. Not only did you waste the time of the Python community needing to close a Too Broad question, but you wasted everyone's time here trying to help you craft an appropriate question when you had no intention of listening to any of the advice given.
    – Servy
    Commented Jun 8, 2017 at 15:12
  • 3
    see Why is "Is it possible to:" a poorly worded question?
    – gnat
    Commented Jun 8, 2017 at 16:42
  • 1
    @Gloin You posted the question despite being told that the question wasn't appropriate. You should have considered the advice and improved your question before posting it, rather than posting it knowing that it was inappropriate and considering fixing it later, maybe.
    – Servy
    Commented Jun 8, 2017 at 20:43
  • 1
    Seems like a question asked out of fear of the unknown. I'm not sure what is going to happen, so I fear bad things are going to happen. Can someone tell me what bad things might happen? Its more productive to focus on how you might know that something bad is happening rather than having some incomplete checklist of things that may or may not occur in real code. Testing and measuring, two important aspects of any software engineer's development routines. Given the popularity of Python, I'm sure there is plenty of help in that area.
    – Gimby
    Commented Jun 9, 2017 at 11:33

2 Answers 2

15

As mentioned in the comments above, the example you're providing is not specific enough. There are lots of things you could possibly do inside a function in any language in general which would cause your program to consume more memory (and then not release it, depending on the circumstances). A couple general ideas come to mind, like if the function is creating/editing objects stored outside of its scope, if you implemented some sort of memory leak, etc. These things are not specific to Python or really any other language either, so the topic you're asking about is going to be extremely broad.

Your best bet would be to find an example of a function (along with the code inside it) which you know does (or conversely, doesn't) increase memory usage on each call, then post that and ask why that particular case produces the results you see instead of whatever you expected.

2
  • 5
    Not "in any language in general". Some functional languages enforce function purity (and reserve the name function for those subroutines which are pure).
    – Ben Voigt
    Commented Jun 8, 2017 at 15:13
  • @BenVoigt That's a good point, and thanks for bringing it up. My personal experience with functional languages has been limited to a brief stint with Haskell in a college course, so I don't consider myself very knowledgeable on that particular topic. Commented Jun 8, 2017 at 15:41
2

I use Python daily, but I wouldn't call myself an expert because I don't know all of the intricate details of the CPython runtime. I think the question is reasonable, not too broad, and can be easily answered with a couple key points.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .