{"id":2967,"date":"2017-11-18T18:01:59","date_gmt":"2017-11-18T18:01:59","guid":{"rendered":"http:\/\/www.csestack.org\/?p=2967"},"modified":"2023-11-30T13:27:11","modified_gmt":"2023-11-30T07:57:11","slug":"python-getrefcount-reference-count-memory-management","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/","title":{"rendered":"Mystery of Python getrefcount() | Reference Count &#038; Memory Management"},"content":{"rendered":"<p>Have you ever wondered-\u00a0How the memory is managed in Python?<\/p>\n<p>Or what is the reference count in Python?<\/p>\n<p>In this article, you can expect detail about the following topics\u2026<\/p>\n<p>Without wasting any further time, let&#8217;s start point by point&#8230;<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg\" alt=\"Python getrefcount reference count\" width=\"640\" height=\"433\" class=\"aligncenter size-full wp-image-2990\" srcset=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg 640w, https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count-300x203.jpg 300w, https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count-600x406.jpg 600w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fd5af3c40ca\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fd5af3c40ca\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#What_is_Python_Reference_Count\" >What is Python Reference Count?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#How_is_the_reference_count_calculated\" >How is the reference count calculated?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#Reference_Count_for_Variable_and_Function\" >Reference Count for Variable and Function:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#Reference_Count_for_Python_List\" >Reference Count for Python List<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#When_does_the_reference_count_increase\" >When does the reference count increase?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#Use_of_the_reference_count_for_Memory_Management_in_Python\" >Use of the reference count for Memory Management in Python:<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#Reference_Count_for_Integer_Immutable_Object\" >Reference Count for Integer (Immutable Object)<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#Lets_play_the_getrefcount_with_different_inputs\" >Lets&#8217; play the getrefcount() with different inputs:<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#Keys_Points_to_remember_about_Python_Reference_Count\" >Keys Points to remember about Python Reference Count:<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 id=\"def\" style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"What_is_Python_Reference_Count\"><\/span>What is Python Reference Count?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For the sake of simplicity, the reference count is nothing but the number of times a Python object is used.<\/p>\n<p>How is the reference count calculated?<\/p>\n<p>Python <code>getrefcount()<\/code> is the function present inbuilt with the Python module\u00a0<code>sys<\/code>. This function takes a Python object as an input and returns the number of references present for the given Python object.<\/p>\n<p>Here, input to the <code>getrefcount()<\/code> can be a variable name, value, function, class, and anything else that comes under a Python object.<\/p>\n<p>Let&#8217;s take an example&#8230;<\/p>\n<pre class=\"brush: python\">import sys\nprint(sys.getrefcount(1556778))<\/pre>\n<p>Output:<\/p>\n<p><samp>3<\/samp><\/p>\n<p>This means the integer value &#8216;1556778&#8217; is used 3 times.<\/p>\n<p>You might be curious&#8230; how does it come 3 times, even if you have used the value only once?<\/p>\n<h3 id=\"calculation\" style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"How_is_the_reference_count_calculated\"><\/span>How is the reference count calculated?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The reference count is calculated based on the two factors&#8230;<\/p>\n<ul>\n<li>Number of times an object is used in the bytecode<\/li>\n<li>If the same object is used earlier, the number of object references from earlier code\u00a0 (can be in the same program or in a background process of Python)<\/li>\n<\/ul>\n<p>Let&#8217;s bend into some technical detail&#8230;<\/p>\n<ol>\n<li><strong>Reference Count from Bytecode:<\/strong><\/li>\n<\/ol>\n<p>When you run any Python program, it gets interpreted into the bytecode. The reference count of the object is calculated based on the number of times an object is used in the bytecode (not from your high-level program code).<\/p>\n<p>You can also check the bytecode of your program using the dis module. It\u00a0<a href=\"http:\/\/www.csestack.org\/how-to-deassemble-python-bytecode\/\">disassembles the Python bytecode<\/a>.<\/p>\n<p>Below is the code to get the bytecode of the Python program.<\/p>\n<pre class=\"brush: python\">import dis\nimport sys\n\nprint(compile(\"sys.getrefcount(1556778)\", '', 'single').co_consts)\nprint(dis.dis(compile(\"sys.getrefcount(1556778)\", '', 'single')))\nprint(sys.getrefcount(1556778))<\/pre>\n<p>Output:<\/p>\n<pre>(1556778, None)\n1 0 LOAD_NAME 0 (sys)\n3 LOAD_ATTR 1 (getrefcount)\n6 LOAD_CONST 0 (1556778)\n9 CALL_FUNCTION 1\n12 PRINT_EXPR\n13 LOAD_CONST 1 (None)\n16 RETURN_VALUE\nNone\n3<\/pre>\n<p>Here, <code>single<\/code> is a mode of Python interpreter.<\/p>\n<p>There are 3 references here-\u00a0one from the\u00a0<a href=\"http:\/\/python-reference.readthedocs.io\/en\/latest\/docs\/code\/consts.html\" target=\"_blank\" rel=\"noopener\"><code>co_consts<\/code><\/a>\u00a0tuple on the code object, one on the stack (from the\u00a0<code>LOAD_CONST<\/code>\u00a0instruction), and one for the <code>sys.getrefcount()<\/code>\u00a0method itself.<\/p>\n<ol start=\"2\">\n<li><strong>Reference Count from other parts of the Code:<\/strong><\/li>\n<\/ol>\n<p>If the same object is used in the other part of the code, it will be counted in the reference count of the given object.<\/p>\n<p>Even, there are multiple cumbersome operations running in background Python. It may be possible that this object is used in the background of your running program. It is also counted as a reference to the object.<\/p>\n<p>The output (reference count) may vary from system to system.<\/p>\n<h3 id=\"var\" style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Reference_Count_for_Variable_and_Function\"><\/span><strong>Reference Count for Variable and Function:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>When you pass the variable as a parameter to the function, the reference count for the variable object is incremented. When the control goes out of the function, the reference count is decremented.<\/p>\n<pre class=\"brush: python\">import sys\na =10\nprint(sys.getrefcount(a)) #17\n\ndef func(b):\n    print(sys.getrefcount(a)) #19\n\nfunc(a)\nprint(sys.getrefcount(a)) #17<\/pre>\n<p><strong>Note:<\/strong> Reference count is shown as 19 instead of 18 because variable &#8216;a&#8217; is used two times in function- as a parameter to the function <code>func()<\/code>; as a parameter to the\u00a0function <code>sys.getrefcount()<\/code>.<\/p>\n<h3 id=\"list\"><span class=\"ez-toc-section\" id=\"Reference_Count_for_Python_List\"><\/span>Reference Count for Python List<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Along with the list object, every element in the list has a separate reference count.<\/p>\n<p>When you delete a list or if the lifetime of the list expires, the reference count of each element in the list goes down by one.<\/p>\n<pre class=\"brush: python\">import sys\n\nliAbc = ['a', 'b', 'c']\n\nprint(sys.getrefcount('a')) #14\nprint(sys.getrefcount('b')) #12\nprint(sys.getrefcount('c')) #23\n\ndel liAbc\n\nprint(sys.getrefcount('a')) #13\nprint(sys.getrefcount('b')) #11\nprint(sys.getrefcount('c')) #22<\/pre>\n<p>For more details about the list, you can <a href=\"http:\/\/www.csestack.org\/difference-tuple-list-python\/\">read Python list vs tuple<\/a>.<\/p>\n<h3 id=\"increase\"><span class=\"ez-toc-section\" id=\"When_does_the_reference_count_increase\"><\/span>When does the reference count increase?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>while assigning an operator<\/li>\n<li>while passing the value as an argument to the function<\/li>\n<li>appending object to the list<\/li>\n<\/ul>\n<h2 id=\"memory-management\" style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Use_of_the_reference_count_for_Memory_Management_in_Python\"><\/span>Use of the reference count for Memory Management in Python:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Python uses dynamic memory allocation. While declaring a variable object, you don&#8217;t need to explicitly allocate the memory. When the object is no longer used in the program, the variable is deleted.<\/p>\n<p>There are two questions that arise&#8230;<\/p>\n<ul>\n<li>While creating the object, what if the object already exists in memory?<\/li>\n<li>While deleting the object, how the system will know if the object is no longer used?<\/li>\n<\/ul>\n<p>And, here comes the use of reference count.<\/p>\n<p><strong>How does Python count references used for Memory Management?<\/strong><\/p>\n<p>Python counts the reference for each object. When you use that object again, the reference count is incremented.<\/p>\n<p>When the reference object comes out of scope, the reference count is decremented.<\/p>\n<p>When the reference count reaches zero, means the Python object is not in use. The memory which is assigned to the object gets deleted.<\/p>\n<h3 id=\"int\" style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Reference_Count_for_Integer_Immutable_Object\"><\/span>Reference Count for Integer (Immutable Object)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The integer is one of the <a href=\"http:\/\/www.csestack.org\/python-numeric-data-types-examples\/\">numeric data types in Python<\/a>.<\/p>\n<p>When you create an integer object, the value of the object is saved in memory to use in the program. The reference count is set.<\/p>\n<p>When you assign the same integer value to another variable, the reference count increases.<\/p>\n<p>It also saves computing resources by using a single place to store the value and assign to all the variables storing the same value in the program.<\/p>\n<p>And we know that an integer is an <a href=\"http:\/\/www.csestack.org\/difference-between-mutable-and-immutable-in-python\/\">immutable datatype in Python<\/a>. So we can not change the value of the integer. The new value is stored in a different memory with the new reference count.<\/p>\n<pre class=\"brush: python\">import sys\nprint(sys.getrefcount(55)) #4\n\nvar = 55\nprint sys.getrefcount(55) #5\n\nvar = var + 1\nprint sys.getrefcount(55) #4<\/pre>\n<p>In the above program, the value of the variable var is incremented (you can change it to any other value or delete the variable). As an Integer is immutable, we can not update the integer value, instead, it stores at a different place and decrements the reference count of the previous value by one.<\/p>\n<p>Now, what if you use a smaller integer value?<\/p>\n<pre class=\"brush: python\">import sys\nprint(sys.getrefcount(1)) #97\nprint(sys.getrefcount(2)) #76\nprint(sys.getrefcount(3)) #30<\/pre>\n<p>This means integer value 1 is used 97 times, 2 is used 76, and 3 is used 30 times.<\/p>\n<p>There are multiple cumbersome operations that go running on a Python background. So these values are used. The output may vary from system to system.<\/p>\n<h2 id=\"graph\" style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Lets_play_the_getrefcount_with_different_inputs\"><\/span>Lets&#8217; play the getrefcount() with different inputs:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To find the pattern for the number of times a Python object is used, we can plot the graph for a range of input objects.<\/p>\n<ol>\n<li>Write a program to plot a graph based on reference count for integer values (say 1 to 500) used in Python.<\/li>\n<\/ol>\n<pre class=\"brush: python\">import sys\nimport matplotlib.pyplot as plt\n\n#calculate the values for x and y axis\nx = range(500)\ny = [sys.getrefcount(i) for i in x]\n\nfig, ax = plt.subplots()\nplt.plot(x, y, '.')\n\n#set lable for x axis\nax.set_xlabel(\"number\")\n#set lable for y axis\nax.set_ylabel(\"sys.<wbr \/>getrefcount(number)\")\n\n#plot the graph\nplt.show()<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-1.png\" alt=\"getrefcount matplotlib graph\" width=\"655\" height=\"480\" class=\"aligncenter size-full wp-image-2969\" srcset=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-1.png 655w, https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-1-300x220.png 300w, https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-1-600x440.png 600w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/p>\n<p>From the graphs, it is clear that there are more numbers of reference counts for smaller numbers. A couple of initial smaller values have a reference count of more than 3000. This means, smaller numbers are used widely running Python in the background.<\/p>\n<ol start=\"2\">\n<li>Similarly, let&#8217;s plot the reference counts graph for 26 English letters.<\/li>\n<\/ol>\n<pre class=\"brush: python\">import sys\nimport matplotlib.pyplot as plt\n\n#string with all character letters\nstrLet = \"abcdefghijklmnopqrstuvwxyz\"\nrefs = [sys.getrefcount(l) for l in strLet]\ny_pos = range(len(strLet))\nplt.bar(y_pos, refs, align='center')\nplt.xticks(y_pos, letters)\n\n#set lable for x axis\nplt.xlabel(\"letter\")\n#set lable for y axis\nplt.ylabel('sys.getrefcount(strLet)')\n\n#plot the graph\nplt.show()<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-for-characters.png\" alt=\"getrefcount matplotlib graph for characters\" width=\"640\" height=\"480\" class=\"aligncenter size-full wp-image-2973\" srcset=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-for-characters.png 640w, https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-for-characters-300x225.png 300w, https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/getrefcount-matplotlib-graph-for-characters-600x450.png 600w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>We are more obsessed with the letter &#8216;x&#8217; and it is used for many variable declarations. If you look at the graph it holds true. The &#8216;x&#8217; as the object is used more than 1100 in Python.<\/p>\n<p>As Python is a case-sensitive language, you will get the difference reference count (so the different graph) for small and big caps letters.<\/p>\n<ol start=\"3\">\n<li>Take some popular keywords in Python, count and plot the reference count.<\/li>\n<\/ol>\n<p>How can we exclude &#8220;Python&#8221; itself?<\/p>\n<pre class=\"brush: python\">import sys\nfor w in [\"python\", \"version\", \"error\", \"var\", \"reference\"]:\n    print w, sys.getrefcount(w)<\/pre>\n<pre>('python', 6)\n('version', 12)\n('error', 47)\n('var', 9)\n('reference', 6)<\/pre>\n<p>You can try some other keywords as well.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Keys_Points_to_remember_about_Python_Reference_Count\"><\/span>Keys Points to remember about Python Reference Count:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>You may get a different reference count for the same object on a different Python system. It solely depends on the number of times an object is used on your system.<\/li>\n<li>If you declare the object as global (declared outside of any block, class or function), can never have a reference count of zero.<\/li>\n<li>The value of the reference count is always one higher than the one you expect as it also counts reference for an object passing to function <code>sys.getrefcount()<\/code> itself.<\/li>\n<li>For Python memory management, the reference count is used. When the reference count of any Python object goes down to zero, the memory assigned to the object is deleted.<\/li>\n<li>You can relate the Reference with the pointer concepts in <a href=\"http:\/\/www.csestack.org\/c-cpp\/\">C programming<\/a>.<\/li>\n<\/ul>\n<p>That&#8217;s all!<\/p>\n<p>Understanding reference count is very important for memory management. If you find this article fruitful, kindly share it with your friends.<\/p>\n<p>I have tried to address answers to multiple daunting questions about Python <code>getrefcount()<\/code> function, reference count, and memory management. If you have any doubts, feel free to write in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How the memory is managed in Python? What is Python getrefcount()? How is reference count calculated? Use of reference count for Python memory management.<\/p>\n","protected":false},"author":1,"featured_media":2990,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,73],"tags":[72],"class_list":["post-2967","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","category-python","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Mystery of Python getrefcount() | Reference Count &amp; Memory Management<\/title>\n<meta name=\"description\" content=\"How the memory is managed in Python? What is Python getrefcount()? How is reference count calculated? Use of reference count for Python memory management.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mystery of Python getrefcount() | Reference Count &amp; Memory Management\" \/>\n<meta property=\"og:description\" content=\"How the memory is managed in Python? What is Python getrefcount()? How is reference count calculated? Use of reference count for Python memory management.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-18T18:01:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-30T07:57:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"433\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aniruddha Chaudhari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aniruddha Chaudhari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/\"},\"author\":{\"name\":\"Aniruddha Chaudhari\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"headline\":\"Mystery of Python getrefcount() | Reference Count &#038; Memory Management\",\"datePublished\":\"2017-11-18T18:01:59+00:00\",\"dateModified\":\"2023-11-30T07:57:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/\"},\"wordCount\":1305,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"image\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/Python-getrefcount-reference-count.jpg\",\"keywords\":[\"Python\"],\"articleSection\":[\"Code\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/\",\"name\":\"Mystery of Python getrefcount() | Reference Count & Memory Management\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/Python-getrefcount-reference-count.jpg\",\"datePublished\":\"2017-11-18T18:01:59+00:00\",\"dateModified\":\"2023-11-30T07:57:11+00:00\",\"description\":\"How the memory is managed in Python? What is Python getrefcount()? How is reference count calculated? Use of reference count for Python memory management.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/Python-getrefcount-reference-count.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/Python-getrefcount-reference-count.jpg\",\"width\":640,\"height\":433,\"caption\":\"Python getrefcount reference count\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-getrefcount-reference-count-memory-management\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mystery of Python getrefcount() | Reference Count &#038; Memory Management\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"],\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/anicse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mystery of Python getrefcount() | Reference Count & Memory Management","description":"How the memory is managed in Python? What is Python getrefcount()? How is reference count calculated? Use of reference count for Python memory management.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/","og_locale":"en_US","og_type":"article","og_title":"Mystery of Python getrefcount() | Reference Count & Memory Management","og_description":"How the memory is managed in Python? What is Python getrefcount()? How is reference count calculated? Use of reference count for Python memory management.","og_url":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_author":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2017-11-18T18:01:59+00:00","article_modified_time":"2023-11-30T07:57:11+00:00","og_image":[{"width":640,"height":433,"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg","type":"image\/jpeg"}],"author":"Aniruddha Chaudhari","twitter_card":"summary_large_image","twitter_creator":"@ani_chaudhari","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Aniruddha Chaudhari","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/"},"author":{"name":"Aniruddha Chaudhari","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"headline":"Mystery of Python getrefcount() | Reference Count &#038; Memory Management","datePublished":"2017-11-18T18:01:59+00:00","dateModified":"2023-11-30T07:57:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/"},"wordCount":1305,"commentCount":0,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"image":{"@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#primaryimage"},"thumbnailUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg","keywords":["Python"],"articleSection":["Code","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/","url":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/","name":"Mystery of Python getrefcount() | Reference Count & Memory Management","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#primaryimage"},"image":{"@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#primaryimage"},"thumbnailUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg","datePublished":"2017-11-18T18:01:59+00:00","dateModified":"2023-11-30T07:57:11+00:00","description":"How the memory is managed in Python? What is Python getrefcount()? How is reference count calculated? Use of reference count for Python memory management.","breadcrumb":{"@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#primaryimage","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2017\/11\/Python-getrefcount-reference-count.jpg","width":640,"height":433,"caption":"Python getrefcount reference count"},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/python-getrefcount-reference-count-memory-management\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"Mystery of Python getrefcount() | Reference Count &#038; Memory Management"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"],"url":"https:\/\/www.csestack.org\/author\/anicse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/2967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=2967"}],"version-history":[{"count":28,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/2967\/revisions"}],"predecessor-version":[{"id":11103,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/2967\/revisions\/11103"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media\/2990"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=2967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=2967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=2967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}