hi there serdar yagalov here for infoworld at idg in this episode of smart python we'll
start exploring the workings of python's automatic memory management mechanisms what's called garbage
collection python has many features to make programming in it convenient and easy among the
biggest the developer doesn't have to manually manage memory as they might in other languages
java has long been attractive for this reason too lets the developer focus on solving problems
with code instead of micromanaging the code's behavior python's memory management system uses
a technique called reference counting every time you create an object in a python program
the python runtime maintains a tally of how many other objects hold a reference or used in some way
that other object when the reference count of an object drops to zero the object is removed from
memory that is it becomes garbage in the parlance of programming and then the memory used by the
object is freed up to be reused by something else the process that we call garbage collection
here's a simple example we create a string and we set a name to point to it right now behind the
scenes isn't where two things have happened we've created a string object somewhere in memory and
we set the name x the variable x to point to the string object so we have some way to refer to that
string so now the string has a reference count we can verify this with sys.getrefcount a function
that returns a number that indicates how many referrers an object has now this count is almost
never just one because getrefcount itself has to take a reference to the object to measure it
and there may be other things in the interpreter that are also holding references to that object
for one reason or another now if we set x to point to something else this string will no
longer have any references and it will vanish from memory now let's look at another example
we create a string and we set a variable to hold it if we also create a dictionary and use the
string as a key the ref count will go up by one if we create a list and insert multiple references to
the string there the ref count will go up one for each reference now watch what happens when
we remove these references for each one that's removed the ref count declines until we're left
with just the original variable reference remove this and our string vanishes all together minus
of course all of the other references behind the scenes that are only for the interpreter's own use
so again every single reference that is made to a given object in memory will increase its ref count
every time you remove such a reference the ref count decreases when a ref count goes to zero the
object is removed the most common way reference counts in object management works is when we
have a function that declares some variables in it that aren't returned from the function
in this example here we take in a string we do some work on it and create some temporary
variables along the way when we're done we return another string but all the temporary variables
that only live inside the function have their references dropped at the end of the function
there's nothing left to point to them so the reference counts are now zero
and they're removed from memory note that this would also happen if we create objects
within other objects if we created a list in this function for instance and stashed something in it
by the end of the function the list would no longer have any references and would vanish
and likewise all of the objects in the list would also no longer have any references left
so they would also vanish the way python handles object references and reference counts are meant
to complement the way most programs work naturally so you don't have to think about
when or how objects have to be removed from memory but it is possible to manually remove references
to objects if you need to it's just that most of the time you don't to that end python provides a
keyword dell short for delete this lets you erase a reference to an object again note that i said
reference delete does not remove instances of an object it only removes a specific reference to it
if something has multiple references you can't remove the underlying object without breaking
all those references so the only thing you can do is to remove specific references manually whether
by reassigning what they point to as we did before or by removing the references altogether
here's an example of this in action we have a string assigned to two variables x and y
if we delete y the string is still available to x all we've done is remove one of the two existing
references to it the string would only vanish from memory entirely if we deleted x and y both
note also by the way that when we use delete on a name that name has been also completely removed
we can't remove something by way of delete and then try to reference it again by that same name
at least not unless we reassign that name to something else so to sum up when you create
an object python keeps track of how many other objects hold a reference to it like a variable
when that count drops to zero the object is removed from memory the vast majority of the
time you don't need to worry about how objects are tracked and removed you can manually remove
references to an object with the dell keyword but again most of the time you don't have to
in the next video i'm going to explore some of the more advanced behaviors of python's memory
management including the programmatic interface to python's garbage collector
this will let you take control of garbage collection behaviors during a program's run
when it's useful for you to do so that's it for this video if you liked it please leave a
comment below don't forget to subscribe to the igg tech talk channel on youtube and for more
dev with serdar and more smart python be sure to follow me on facebook youtube and infoworld.com