[Tutor] python memory management

Steven D'Aprano steve at pearwood.info
Sat Sep 3 12:35:24 EDT 2016


On Thu, Sep 01, 2016 at 08:21:36PM +0000, monikajg at netzero.net wrote:

> Thank you for your explanation. It is very clear and confirms what I 
> thought I knew. However, I had a job interview and the interview said 
> it was a mistake that I did not say that in cases when there are 
> multiple programmers, there might be some objects/references left and 
> not deallocated from the memory by python. He asked me how this should 
> be handles.

Without being there, I cannot be sure precisely what he meant. But 
frankly it sounds like the interviewer doesn't know what he is talking 
about. The number of programmers who work on a project has nothing to do 
with how it manages memory. Whether you have one person who writes the 
code, or a hundred people writing the code, memory is managed exactly 
the same way.

Perhaps you misunderstood the question, or perhaps the interviewer 
simply wasn't as knowledgable or smart as he thought he was.


> So I told him that python has its automatic garbage collection which 
> keeps track of all objects and references and deletes them as 
> appropriate (similar what you state). I added that programmers can 
> keep track of occurrences of object and make sure that it goes down to 
> 0. And if it does not then use del to delete. However, he did not like 
> my answer.

What you say is technically correct, but if you find yourself using del 
more than very occasionally, I suggest that you need to think hard about 
what you are doing. It is not normally necessary to use del.


> So Im trying to learn from my mistakes and learn if an 
> object or reference are still at the end of the program how they 
> should be deleted.

A thought comes to mind... 

When Python shuts down, it has to deallocate all your remaining objects, 
giving them a chance to run any finaliser methods. But sometimes the 
finaliser methods don't get to run until Python has already deallocated 
the global variables, including modules you might want to use. So for 
example:

import math

class Widget:
    def __del__(self):
        # Finaliser
        print(math.sin(10))


but if your widget doesn't get deallocated until the end of your 
application, it may be that math has been set to None and math.sin is no 
longer available.

Perhaps he was asking about that?

What sort of job were you asking for? I expect that would be counted as 
an extremely advanced corner of Python, not something that most people 
know or need to know.

> I had problem understanding him too well since he 
> was calling from overseas (there was interference) and I could not 
> understand his accent well. But I need to learn this for future 
> interviews.
>
> Your posting states that this is not a problem but according to the 
> interviewer it is. (I do not mean to be un-nice to you, sorry) So how 
> this situation should be handled?

There's no way to know the right way.

Some interviewers think they know more than they actually do. When you 
tell them the right answer, they think it is wrong because they are 
ignorant.

Some interviewers are deliberately testing you with really obscure, 
complex corners of the language. Maybe he was right, and there is some 
tiny corner where what I told you was incorrect.

Maybe you misunderstood the question.

Maybe the interviewer was deliberately testing you with false 
information, to see what you would do. Would you argue with him? Get 
frustrated? Agree with him even if you knew the answer was actually 
wrong?

There's no way of knowing what they are looking for. Do they want people 
who will push back and stand their ground when they know they are right, 
or people who will be good little employees who will agree when the boss 
says that milk is purple and England is the capital of China?


I guess the only thing you can do is answer as best you can, ask for 
clarifications, and hope.


-- 
Steve


More information about the Tutor mailing list