{"id":79822,"date":"2022-08-12T12:56:43","date_gmt":"2022-08-12T12:56:43","guid":{"rendered":"https:\/\/ded9.com\/?p=79822"},"modified":"2025-11-04T08:34:06","modified_gmt":"2025-11-04T08:34:06","slug":"what-is-garbage-collection-in-python-and-how-does-it-work","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/what-is-garbage-collection-in-python-and-how-does-it-work\/","title":{"rendered":"What Is Garbage Collection in Python &#038; How Does It Work?"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Python is one of the most popular programming languages used to build various projects. In 2021, The Language Was Ranked Third On The TIOBE Website List Due To Public Acceptance.\u00a0<\/span><\/p>\n<p>Ease of use and support by a large community of Python developers make it a good choice for data science, building web-based applications, and more.<\/p>\n<p>One of the critical concepts around the Python programming language that is less discussed is &#8220;Garbage Collection.&#8221;<\/p>\n<p>A Python programmer must thoroughly understand the principles of memory management and why garbage collection is used in this language.<\/p>\n<p>Accordingly, this article will learn how the garbage collection mechanism works in Python. We will continue to introduce points you should pay attention to when writing Python programs and using the mentioned features.<\/p>\n<h2>What is garbage collection in Python, and why do we need it?<\/h2>\n<p>If\u00a0<strong>Python<\/strong>\u00a0is your first\u00a0<strong>programming language<\/strong>, you may not know much about the concept of garbage collection, so let&#8217;s start with the basics.<\/p>\n<h2>Memory Management<\/h2>\n<p>A\u00a0<strong>programming language<\/strong> uses objects to perform various operations.\u00a0Objects can be simple variables such as strings, integers, logical, and more complex data structures such as lists, hashes, or classes.<\/p>\n<p>The values \u200b\u200bof objects your application uses are stored in memory for quick access. In most programming languages, a variable defined in a program is a pointer to the memory address of an object. When a variable is used in a program, the application process reads the value that the variable points to in memory and operates on it.<\/p>\n<p>In early programming languages, programmers were responsible for managing the memory used by the application. So, before creating a list or an object, the memory for the variable should be specified. After finishing the work with the variable, it would be deleted from the memory to free the allocated memory. If this is not done, the application would quickly consume the system&#8217;s free memory, and the system&#8217;s performance would suffer a severe drop due to the lack of free memory. For example, when pointers are defined dynamically in the C programming language, you must clear them after the work is done so that the memory used by the pointers is freed and returned to the system.<\/p>\n<h2>What are the consequences of not freeing memory?<\/h2>\n<p>If you don&#8217;t release the system&#8217;s main memory after using it, the &#8220;Memory Leak&#8221; problem will arise. Over time, this problem causes the application to use too much memory and creates security problems if the application is used for a long time. In this case, the application is still in memory and requires memory to perform certain activities, but no memory is reserved for the application.<\/p>\n<p>Another problem is freeing the allocated memory incorrectly. More precisely, if the memory used by the application is freed without special precautions, it will still cause problems for the application. This issue creates two big problems: The application may stop running, or data used by the application may be corrupted. A variable that points to freed memory is called a &#8220;dangling pointer.&#8221; Such problems caused companies to think of new solutions for automatic memory management in modern programming languages. This approach eventually led to the invention of automated memory management and garbage collection technology.<\/p>\n<h2>Automatic memory management and garbage collection<\/h2>\n<p>With automatic memory management, programmers no longer need to manage memory, and a component called &#8220;Runtime&#8221; contains this process. There are several methods for automated memory management. The most popular way is reference counting. In the reference counting method, the runtime component keeps track of all references to an object. When an object has no connections, it is labeled &#8220;unusable&#8221; by the runtime component.<\/p>\n<p>Automatic memory management provides several significant advantages to programmers. Programmers can focus solely on the application&#8217;s business logic without worrying about low-level memory details. Automated memory management is an efficient solution to prevent memory leaks and dangling pointers.<\/p>\n<p>However, automatic memory management comes at a cost.\u00a0Your program must use additional memory and computation to track all the references.\u00a0In addition, most programming languages \u200b\u200bmust use a stop-the-world process for garbage collection to take advantage of the automatic memory management technique.\u00a0In this process, some execution operations are temporarily stopped so that the garbage collector component can accurately identify and collect unused objects.<\/p>\n<p>Thanks to Moore&#8217;s Law, advances in CPU core optimization, and the installation of multi-gigabyte main memory modules in personal computers, the advantages of automatic memory management outweigh its disadvantages. Most modern programming languages, such as Java, Python, and Golang, use automatic memory management mechanisms.<\/p>\n<p>Of course, some languages \u200b\u200bstill use manual memory management mechanisms, including <a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B\" target=\"_blank\" rel=\"noopener\">C++<\/a>, Objective-C, and <a href=\"https:\/\/ded9.com\/rust-vs-go-which-one-to-choose\/\">Rust<\/a>. Now that we have an overview of memory management and garbage collection, let&#8217;s explore the garbage collection mechanism in <strong>Python<\/strong>.<\/p>\n<h2><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-79826\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/08\/word-image-79822-1.jpeg\" alt=\"Automatic memory management and garbage collection\" width=\"835\" height=\"438\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2022\/08\/word-image-79822-1.jpeg 835w, https:\/\/ded9.com\/wp-content\/uploads\/2022\/08\/word-image-79822-1-300x157.jpeg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2022\/08\/word-image-79822-1-768x403.jpeg 768w\" sizes=\"(max-width: 835px) 100vw, 835px\" \/><\/h2>\n<h2>How Python uses the garbage collection mechanism<\/h2>\n<p>Suppose we have installed an implementation of\u00a0<strong>Python<\/strong>\u00a0called\u00a0<strong>CPython<\/strong>\u00a0on a system.\u00a0CPython is one of the most widely used\u00a0<strong>Python<\/strong> implementations by programmers worldwide. Of course, other implementations of\u00a0<strong>Python,<\/strong> such as PyPy, Jython (based on Java), or IronPython (based on C# ), have their uses. To see what Python is installed on your operating system, run the following command in the Linux terminal:<\/p>\n<p>&gt;&gt;&gt;python -c \u2018import platform; print(platform.python_implementation())\u2019<\/p>\n<p>Or, you can have these lines for both Linux and Windows terminals.<\/p>\n<p>&gt;&gt;&gt; import platform<\/p>\n<p>&gt;&gt;&gt; print(platform.python_imlplementation())<\/p>\n<p>CPython<\/p>\n<p>C\u00a0<strong>Python<\/strong> manages memory and garbage collection\u00a0based on reference counting and generational\u00a0<strong>garbage collection.<\/strong><\/p>\n<h2>Reference counting in CPython<\/h2>\n<p>The primary garbage collection mechanism in C <strong>Python<\/strong>\u00a0is reference counting.\u00a0Whenever you create an object in C\u00a0<strong>Python<\/strong>, the created thing has both a <b>Python-type<\/b>\u00a0attribute (such as a list, dictionary, or function) and a reference count.<\/p>\n<p>In primary mode, whenever a reference is made to an object, the reference counter is incremented by one unit. When a reference to the object is terminated, the counter is decremented by one unit. If the reference count to an object is 0, the memory allocated to the object is freed. <strong>Note that your application cannot disable Python&#8217;s<\/strong>\u00a0reference counting pattern. Some developers claim that the reference counting feature performs poorly and has flaws. For example, the referral recognition cycle sometimes fails to function correctly. However, reference counting has acceptable performance because it can immediately remove an object when there are no references to it, freeing up the associated memory.<\/p>\n<h2>See the number of references in Python.<\/h2>\n<p>Assign an object to a variable. You can use the Python standard library sys module\u00a0to check the number of references to a particular object.\u00a0The thing you should pay attention to in this context is that the process of increasing the number of references to an object has special conditions as follows:<\/p>\n<ul>\n<li><\/li>\n<li>\u00a0I am adding an object to a data structure, such as adding to a list or adding as a property on a class instance.<\/li>\n<li>\u00a0You are passing an object as an argument to a function.<\/li>\n<\/ul>\n<p>To better understand this, we&#8217;ll use a <strong>Python<\/strong>\u00a0REPL and the sys module to take a closer look.\u00a0First, in the operating system terminal, type\u00a0<strong>Python to bring up the Python<\/strong> REPL window.<\/p>\n<h4>Include the sys module in the REPL, create a variable, and check its reference count:<\/h4>\n<p>&gt;&gt;&gt; import sys<\/p>\n<p>&gt;&gt;&gt; a = \u2018my-string\u2019<\/p>\n<p>&gt;&gt;&gt; sys.get recount(a)<\/p>\n<p>2<\/p>\n<p>In the code snippet above, there are two references to the variable a. The first reference is created when the variable is made, and the second is when we pass the variable as a parameter to the sys. get recount() function. If you add the variable to a data structure such as a list or dictionary, you will still see an increase in the number of references.<\/p>\n<h4>The following code snippet illustrates this:<\/h4>\n<p>&gt;&gt;&gt; import sys<\/p>\n<p>&gt;&gt;&gt; a = \u2018my-string\u2019<\/p>\n<p>&gt;&gt;&gt; b = [a] # Make a list with a as an element.<\/p>\n<p>&gt;&gt;&gt; c = &#8216;\u2018key&#8217;: a } # Create a dictionary with a as one of the values.<\/p>\n<p>&gt;&gt;&gt; sys.get recount(a)<\/p>\n<p>4<\/p>\n<p>As you can see, the number of references associated with the variable increases when the variable is added to a list or dictionary.<\/p>\n<p>Now that we have seen how the functional mechanism of the reference counter works, it is time to go to the generational garbage collector technique, the second <strong>Python<\/strong>\u00a0tool for memory management.<\/p>\n<h2>generational garbage collector<\/h2>\n<p>In addition to the reference counting strategy for memory management,\u00a0<strong>Python<\/strong>\u00a0uses a generational garbage collector mechanism.\u00a0The easiest way to understand why we need the above feature is to give an example.\u00a0In the previous section, we saw that adding an object to an array or an object increases its reference count, but what happens if you add an object to itself?\u00a0Pay attention to the following code snippet:<\/p>\n<p>&gt;&gt;&gt; class MyClass(object):<\/p>\n<p>&#8230; \u00a0\u00a0\u00a0\u00a0pass<\/p>\n<p>&#8230;<\/p>\n<p>&gt;&gt;&gt; a = MyClass()<\/p>\n<p>&gt;&gt;&gt; a.obj = a<\/p>\n<p>&gt;&gt;&gt; del a<\/p>\n<p>In the example above, we defined a new class. Next, we create an instance of the course and assign it a model that is a property of the object itself. However,\u00a0<strong>Python<\/strong>\u00a0has not released this instance from memory, so its reference count is not zero because the thing refers to itself. Finally, we removed the sample. Deleting the piece makes it no longer possible to use it in the Python program.<\/p>\n<p>We refer to this type of problem as a &#8220;reference cycle.&#8221; Unfortunately, the above problem cannot be solved through the reference counting mechanism. It is precisely when the generational garbage collector feature comes into play\u2014a utility feature available through the GC module in the <strong>Python<\/strong> standard library.<\/p>\n<h2>Functional terms<\/h2>\n<p>There are two essential terms to be aware of when discussing the Generational garbage collector feature. The first term represents a generation, and the second term represents the threshold.<\/p>\n<p>The garbage collector keeps all objects in memory. The life cycle of a new thing starts with the first generation of garbage collectors. <strong>Python<\/strong>&#8216;s garbage collection mechanism has three generations; whenever an object survives the garbage collection process in its current age, it is passed to the next generation. If Python runs a garbage collection process on one generation and an object is active, it is redirected to the second generation.<\/p>\n<p>The garbage collection module has a &#8220;threshold&#8221; number of objects for each generation. The garbage collector executes the collection process if the number of things exceeds that threshold. Any object that survives this process is passed on to the next generation.<\/p>\n<p>The good thing about the above technique is that programmers can manually run the garbage collection process or turn off the garbage collection process altogether. To clarify the discussion, let&#8217;s use the GC module to check the performance of the mentioned feature.<\/p>\n<h2>\u00a0Using the GC module<\/h2>\n<p>In the Linux terminal, type\u00a0<strong>Python<\/strong>\u00a0to go to the\u00a0<strong>Python<\/strong> REPL.\u00a0Add the GC module to your session.\u00a0Call the get_threshold method to check how garbage is collected:<\/p>\n<p>&gt;&gt;&gt; import GC<\/p>\n<p>&gt;&gt;&gt; GC.get_threshold()<\/p>\n<p>(700, 10, 10)<\/p>\n<p>By default,\u00a0<strong>Python<\/strong>\u00a0sets a threshold of 700 for the newest generation and 10 for the two older generations.<\/p>\n<h4>With the get_count method, you can check the number of objects in each generation, as in the following code snippet:<\/h4>\n<p>&gt;&gt;&gt; import GC<\/p>\n<p>&gt;&gt;&gt; GC.get_count()<\/p>\n<p>(596, 2, 1)<\/p>\n<p>As you can see,\u00a0<strong>Python<\/strong> creates several objects by default before the program runs.<\/p>\n<h4>Using the GC. Collect method, and you can run the manual garbage collection process as in the following code snippet:<\/h4>\n<p>&gt;&gt;&gt; GC.get_count()<\/p>\n<p>(595, 2, 1)<\/p>\n<p>&gt;&gt;&gt; GC.collect()<\/p>\n<p>577<\/p>\n<p>&gt;&gt;&gt; GC.get_count()<\/p>\n<p>(18, 0, 0)<\/p>\n<p>Executing a garbage collection process causes the application to release a significant portion of unused objects.<\/p>\n<p>Of course, it is possible that the method.<\/p>\n<h4>Use set_threshold in the GC module to change the threshold to start garbage collection:<\/h4>\n<p>&gt;&gt;&gt; import GC<\/p>\n<p>&gt;&gt;&gt; GC.get_threshold()<\/p>\n<p>(700, 10, 10)<\/p>\n<p>&gt;&gt;&gt; gc.set_threshold(1000, 15, 15)<\/p>\n<p>&gt;&gt;&gt; gc.get_threshold()<\/p>\n<p>(1000, 15, 15)<\/p>\n<p>In the code snippet above, we have increased the value of each of the default thresholds. Expanding the point reduces the garbage collector&#8217;s workload, which improves performance. Of course, the problem with the above technique is that the program will accept more unreferenced objects. Now that we know how reference counting and the garbage collection module work, it&#8217;s time to learn how to use them when writing <strong>Python<\/strong> programs.<\/p>\n<h2>Why is the way Python&#8217;s garbage collector works essential for better coding?<\/h2>\n<p>Now that we understand how memory is managed and how to collect unused objects, let&#8217;s move on to how to utilize this information as a <strong>Python<\/strong> application developer.<\/p>\n<p>General rule: don&#8217;t try to change the working pattern of the garbage collector.<\/p>\n<p><strong>Generally, you shouldn&#8217;t consider changing how Python&#8217;s<\/strong>\u00a0garbage collection works. One of the critical benefits of\u00a0<strong>Python<\/strong>\u00a0is increased developer productivity, as it helps developers focus on the application&#8217;s business logic by abstracting away technical details.<\/p>\n<p>Manual memory management is more beneficial for specific projects. Suppose you encounter performance limitations that you think may be related to <b>Python&#8217;s<\/b>\u00a0garbage collection mechanisms. In that case, it&#8217;s best to focus on improving your coding patterns instead of manually changing the garbage collection process. In most cases, rewriting the code and using alternative objects will achieve the desired result. Also, manual garbage collection to free up memory may produce unexpected results.<\/p>\n<h2>Disable Garbage Collector<\/h2>\n<p>In some projects, you have to turn off the garbage collection process from automatic mode and manage it manually. One thing to note in this context is that the reference counting feature is Python&#8217;s primary garbage collection mechanism that cannot be disabled. The only instrument you can change is the generational garbage collector.<\/p>\n<p>It is in the GC module. An exciting application example in this field was Instagram, which disabled the garbage collector feature. Instagram uses Django, the popular <strong>Python<\/strong> web framework, to develop its web applications so that it can run multiple instances of its web application on a single compute instance. These instances are executed using a master-child mechanism, where the child uses memory shared with the master.<\/p>\n<p>The Instagram development team noticed that the shared memory after creating a child decreases drastically.<\/p>\n<p>Evaluations revealed that the issue is with the Garbage Collector. The Instagram team turned off the garbage collection module by setting the thresholds to zero for all generations, which made their web apps 10% more efficient.<\/p>\n<p>While the above example is interesting, before following this path in your application projects, ensure that the application&#8217;s performance problem is related to the garbage collection feature. Instagram is a web-scale application that serves millions of users. For this reason, they can edit some behavioral patterns and use non-standard mechanisms to achieve greater productivity. In most cases, <b>Python&#8217;s<\/b>\u00a0standard functionality meets business needs.<\/p>\n<h3>last word<\/h3>\n<p>To correctly manage garbage collection in Python, you should conduct thorough research. To this end, use tools like Stackify&#8217;s Retrace to evaluate your app&#8217;s performance and pinpoint issues. Once you fully understand the problem, could you take the necessary steps to fix it?<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is garbage collection in Python?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Garbage collection in Python is an automatic memory management process that frees up memory by deleting objects that are no longer referenced or needed by the program.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How does Python\u2019s garbage collector work?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Python primarily uses reference counting to track object usage, and it also employs a cyclic garbage collector to detect and clean up reference cycles that reference counting alone cannot handle.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why is garbage collection important in Python programming?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It helps prevent memory leaks, ensures efficient use of memory resources, and allows developers to focus on writing code without manually managing memory.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python is one of the most popular programming languages used to build various projects. In 2021, The Language Was Ranked Third On The TIOBE Website List Due To Public Acceptance.\u00a0 Ease of use and support by a large community of Python developers make it a good choice for data science, building web-based applications, and more. [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":79823,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[316],"tags":[1066,11925],"class_list":["post-79822","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-c","tag-rust"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/79822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=79822"}],"version-history":[{"count":5,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/79822\/revisions"}],"predecessor-version":[{"id":264820,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/79822\/revisions\/264820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/79823"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=79822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=79822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=79822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}