Search code examples
pythoninstance

does a instance expire once used?


I'm new-ish to Python and know enough to make things happen but still learning some of the core concepts.

So todays question is ... when i make a instance of something. is it a temporary thing that vanishes after its been used or does it sit in memory taking up space ?

The reason i went down this thought process is i have a timer which repeats every 2 minutes. In the method that the timer triggers it makes a instance of something, does what it needs to then in 2 minutes it repeats. So would that mean somewhere in memory i'm adding a instance beside a instance beside a instance every 2 minutes or am i over thinking it :)

I've googled for answers but due to the nature of the question i'm struggling to find someone whos asked similar


Solution

  • Python is garbage collected. This means that a garbage collector engine automatically checks for unused objects and frees them up in memory. This has its pros and cons. The pro is that you don’t have to bother about memory leaks (loss of available memory because the space for unused objects isn’t freed up, so you’re gradually losing more and more available memory as your program proceeds), which is something that may happen in other languages such as C where you’re expected to free() unused memory yourself. The con is that the garbage collector itself takes up resources to run.