{"id":823,"date":"2020-10-26T11:39:36","date_gmt":"2020-10-26T11:39:36","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=823"},"modified":"2025-03-27T13:21:06","modified_gmt":"2025-03-27T13:21:06","slug":"python-garbage-collection","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-garbage-collection\/","title":{"rendered":"Python Garbage Collection"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how Python garbage collection works and how to interact with the garbage collector programmatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-garbage-collection'>Introduction to Python garbage collection <a href=\"#introduction-to-python-garbage-collection\" class=\"anchor\" id=\"introduction-to-python-garbage-collection\" title=\"Anchor for Introduction to Python garbage collection\">#<\/a><\/h2>\n\n\n\n<p>In C\/C++, you&#8217;re fully responsible for managing the memory of the program. However, in Python, you don&#8217;t have to manage the memory yourself because Python does it for you automatically.<\/p>\n\n\n\n<p>In the <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-references\/\">references tutorial<\/a>, you&#8217;ve learned that Python Memory Manager keeps track of references of objects. The Memory Manager destroys the object and reclaims the memory once the reference count of that object reaches zero.<\/p>\n\n\n\n<p>However, reference counting doesn&#8217;t work properly all the time. For example, when you have an object that references itself or two objects reference each other. This creates something called circular references.<\/p>\n\n\n\n<p>When the Python Memory Manager cannot remove objects with circular references, it causes a memory leak.<\/p>\n\n\n\n<p>This is why the garbage collector comes into play to fix the circular references.<\/p>\n\n\n\n<p>Python allows you to interact with the garbage collector via the built-in <code>gc<\/code> module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='interacting-with-python-garbage-collector'>Interacting with Python garbage collector <a href=\"#interacting-with-python-garbage-collector\" class=\"anchor\" id=\"interacting-with-python-garbage-collector\" title=\"Anchor for Interacting with Python garbage collector\">#<\/a><\/h2>\n\n\n\n<p>In this example, we&#8217;ll first create a circular reference between two instances of class A and class B. Then, we use the garbage collector to destroy the objects in the circular reference.<\/p>\n\n\n\n<p>First, import the <code>gc<\/code> and <code>ctypes<\/code> modules and defines two functions for counting references and check if an object exists in the memory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> gc\n<span class=\"hljs-keyword\">import<\/span> ctypes\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">ref_count<\/span><span class=\"hljs-params\">(address)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> ctypes.c_long.from_address(address).value\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">object_exists<\/span><span class=\"hljs-params\">(object_id)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> object <span class=\"hljs-keyword\">in<\/span> gc.get_objects():\n        <span class=\"hljs-keyword\">if<\/span> id(object) == object_id:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this code, the <code>ref_count()<\/code> returns the reference count of an object specified by its memory address. And the <code>object_exists()<\/code> function returns <code>True<\/code> if an object exists in the memory.<\/p>\n\n\n\n<p>Second, create two <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">classes<\/a> <code>A<\/code> and <code>B<\/code> that have a reference each other:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">A<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.b = B(self)\n        print(<span class=\"hljs-string\">f'A: <span class=\"hljs-subst\">{hex(id(self))}<\/span>, B: <span class=\"hljs-subst\">{hex(id(self.b))}<\/span>'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">B<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, a)<\/span>:<\/span>\n        self.a = a\n        print(<span class=\"hljs-string\">f'B: <span class=\"hljs-subst\">{hex(id(self))}<\/span>, A: <span class=\"hljs-subst\">{hex(id(self.a))}<\/span>'<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, disable the garbage collector by calling the <code>disable()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">gc.disable()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, create a new instance of class A that also automatically creates a new instance of B:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = A()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">B: <span class=\"hljs-number\">0x20fccd148e0<\/span>, A: <span class=\"hljs-number\">0x20fccd75c40<\/span>\nA: <span class=\"hljs-number\">0x20fccd75c40<\/span>, B: <span class=\"hljs-number\">0x20fccd148e0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, define two variables to hold the memory addresses of the instances of A and B. These variables keep track of the memory addresses of instances of A and B when the variable <code>a<\/code> references another object.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a_id = id(a)\nb_id = id(a.b)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, show the reference counts of instances of A and B:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(ref_count(a_id))  <span class=\"hljs-comment\"># 2<\/span>\nprint(ref_count(b_id))  <span class=\"hljs-comment\"># 1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The instance of A has two references which is the variable <code>a<\/code> and the instance of B. And the instance of B has one reference which is the instance of A.<\/p>\n\n\n\n<p>Seventh, check if both instances of A and B are in the memory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(object_exists(a_id))  <span class=\"hljs-comment\"># True<\/span>\nprint(object_exists(b_id))  <span class=\"hljs-comment\"># True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Both of them exist.<\/p>\n\n\n\n<p>Eighth, set a variable to <code>None<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-literal\">None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Ninth, get the reference counts of the instance of A and B:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(ref_count(a_id))  <span class=\"hljs-comment\"># 1<\/span>\nprint(ref_count(b_id))  <span class=\"hljs-comment\"># 1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, both reference count of the instance of A and B is 1.<\/p>\n\n\n\n<p>Tenth, check if the instances exist:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(object_exists(a_id))  <span class=\"hljs-comment\"># True<\/span>\nprint(object_exists(b_id))  <span class=\"hljs-comment\"># True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Both of them still exist as expected.<\/p>\n\n\n\n<p>Eleventh, start the garbage collector:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">gc.collect()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When the garbage collector runs, it can detect the circular reference, destroy the objects, and reclaim the memory.<\/p>\n\n\n\n<p>Twelveth, check if the instances of A and B exist:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(object_exists(a_id))  <span class=\"hljs-comment\"># False<\/span>\nprint(object_exists(b_id))  <span class=\"hljs-comment\"># False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Both of them don&#8217;t exist anymore due to the garbage collector.<\/p>\n\n\n\n<p>Thirteenth, get the reference counts of the instances of A and B:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(ref_count(a_id))  <span class=\"hljs-comment\"># 0<\/span>\nprint(ref_count(b_id))  <span class=\"hljs-comment\"># 0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Put it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> gc\n<span class=\"hljs-keyword\">import<\/span> ctypes\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">ref_count<\/span><span class=\"hljs-params\">(address)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> ctypes.c_long.from_address(address).value\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">object_exists<\/span><span class=\"hljs-params\">(object_id)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> object <span class=\"hljs-keyword\">in<\/span> gc.get_objects():\n        <span class=\"hljs-keyword\">if<\/span> id(object) == object_id:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">A<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.b = B(self)\n        print(<span class=\"hljs-string\">f'A: <span class=\"hljs-subst\">{hex(id(self))}<\/span>, B: <span class=\"hljs-subst\">{hex(id(self.b))}<\/span>'<\/span>)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">B<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, a)<\/span>:<\/span>\n        self.a = a\n        print(<span class=\"hljs-string\">f'B: <span class=\"hljs-subst\">{hex(id(self))}<\/span>, A: <span class=\"hljs-subst\">{hex(id(self.a))}<\/span>'<\/span>)\n\n\n<span class=\"hljs-comment\"># disable the garbage collector<\/span>\ngc.disable()\n\na = A()\n\na_id = id(a)\nb_id = id(a.b)\n\nprint(ref_count(a_id))  <span class=\"hljs-comment\"># 2<\/span>\nprint(ref_count(b_id))  <span class=\"hljs-comment\"># 1<\/span>\n\nprint(object_exists(a_id))  <span class=\"hljs-comment\"># True<\/span>\nprint(object_exists(b_id))  <span class=\"hljs-comment\"># True<\/span>\n\n\na = <span class=\"hljs-literal\">None<\/span>\nprint(ref_count(a_id))  <span class=\"hljs-comment\"># 1<\/span>\nprint(ref_count(b_id))  <span class=\"hljs-comment\"># 1<\/span>\n\nprint(object_exists(a_id))  <span class=\"hljs-comment\"># True<\/span>\nprint(object_exists(b_id))  <span class=\"hljs-comment\"># True<\/span>\n\n<span class=\"hljs-comment\"># run the garbage collector<\/span>\ngc.collect()\n\n<span class=\"hljs-comment\"># check if object exists<\/span>\nprint(object_exists(a_id))  <span class=\"hljs-comment\"># False<\/span>\nprint(object_exists(b_id))  <span class=\"hljs-comment\"># False<\/span>\n\n<span class=\"hljs-comment\"># reference count<\/span>\nprint(ref_count(a_id))  <span class=\"hljs-comment\"># 0<\/span>\nprint(ref_count(b_id))  <span class=\"hljs-comment\"># 0<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=aW1wb3J0IGdjCmltcG9ydCBjdHlwZXMKCgpkZWYgcmVmX2NvdW50KGFkZHJlc3MpOgogICAgcmV0dXJuIGN0eXBlcy5jX2xvbmcuZnJvbV9hZGRyZXNzKGFkZHJlc3MpLnZhbHVlCgoKZGVmIG9iamVjdF9leGlzdHMob2JqZWN0X2lkKToKICAgIGZvciBvYmplY3QgaW4gZ2MuZ2V0X29iamVjdHMoKToKICAgICAgICBpZiBpZChvYmplY3QpID09IG9iamVjdF9pZDoKICAgICAgICAgICAgcmV0dXJuIFRydWUKCiAgICByZXR1cm4gRmFsc2UKCgpjbGFzcyBBOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuYiA9IEIoc2VsZikKICAgICAgICBwcmludChmJ0E6IHtoZXgoaWQoc2VsZikpfSwgQjoge2hleChpZChzZWxmLmIpKX0nKQoKCmNsYXNzIEI6CiAgICBkZWYgX19pbml0X18oc2VsZiwgYSk6CiAgICAgICAgc2VsZi5hID0gYQogICAgICAgIHByaW50KGYnQjoge2hleChpZChzZWxmKSl9LCBBOiB7aGV4KGlkKHNlbGYuYSkpfScpCgoKIyBkaXNhYmxlIHRoZSBnYXJiYWdlIGNvbGxlY3RvcgpnYy5kaXNhYmxlKCkKCmEgPSBBKCkKCmFfaWQgPSBpZChhKQpiX2lkID0gaWQoYS5iKQoKcHJpbnQocmVmX2NvdW50KGFfaWQpKSAgIyAyCnByaW50KHJlZl9jb3VudChiX2lkKSkgICMgMQoKcHJpbnQob2JqZWN0X2V4aXN0cyhhX2lkKSkgICMgVHJ1ZQpwcmludChvYmplY3RfZXhpc3RzKGJfaWQpKSAgIyBUcnVlCgoKYSA9IE5vbmUKcHJpbnQocmVmX2NvdW50KGFfaWQpKSAgIyAxCnByaW50KHJlZl9jb3VudChiX2lkKSkgICMgMQoKcHJpbnQob2JqZWN0X2V4aXN0cyhhX2lkKSkgICMgVHJ1ZQpwcmludChvYmplY3RfZXhpc3RzKGJfaWQpKSAgIyBUcnVlCgojIHJ1biB0aGUgZ2FyYmFnZSBjb2xsZWN0b3IKZ2MuY29sbGVjdCgpCgojIGNoZWNrIGlmIG9iamVjdCBleGlzdHMKcHJpbnQob2JqZWN0X2V4aXN0cyhhX2lkKSkgICMgRmFsc2UKcHJpbnQob2JqZWN0X2V4aXN0cyhiX2lkKSkgICMgRmFsc2UKCiMgcmVmZXJlbmNlIGNvdW50CnByaW50KHJlZl9jb3VudChhX2lkKSkgICMgMApwcmludChyZWZfY291bnQoYl9pZCkpICAjIDA%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python automatically manages memory for you using reference counting and garbage collector.<\/li>\n\n\n\n<li>Python garbage collector helps you avoid memory leaks by detecting circular references and destroy objects appropriately.<\/li>\n\n\n\n<li>Never disable the garbage collector unless you have a good reason to do so.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"823\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-garbage-collection\/\"\n\t\t\t\tdata-post-title=\"Python Garbage Collection\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"823\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-garbage-collection\/\"\n\t\t\t\tdata-post-title=\"Python Garbage Collection\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how Python garbage collection works and how to interact with the garbage collector.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-823","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/823\/revisions"}],"predecessor-version":[{"id":7102,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/823\/revisions\/7102"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}