<p>How would you go about finding out how much memory is being used by an object? I know it is possible to find out how much is used by a block of code, but not by an instantiated object (anytime during its life), which is what I want. </p>

[{'answer_id': 34011, 'author': 'jcsalterego', 'author_id': 1416, 'author_profile': 'https://Stackoverflow.com/users/1416', 'pm_score': 3, 'selected': False, 'text': '<p>I haven\'t any personal experience with either of the following, but a simple search for a "Python [memory] profiler" yield:</p>\n\n<ul>\n<li><p>PySizer, "a memory profiler for Python," found at <a href="http://pysizer.8325.org/" rel="noreferrer">http://pysizer.8325.org/</a>.  However the page seems to indicate that the project hasn\'t been updated for a while, and refers to...</p></li>\n<li><p>Heapy, "support[ing] debugging and optimization regarding memory related issues in Python programs," found at <a href="http://guppy-pe.sourceforge.net/#Heapy" rel="noreferrer">http://guppy-pe.sourceforge.net/#Heapy</a>.</p></li>\n</ul>\n\n<p>Hope that helps.</p>\n'}, {'answer_id': 35645, 'author': 'fserb', 'author_id': 3702, 'author_profile': 'https://Stackoverflow.com/users/3702', 'pm_score': 8, 'selected': True, 'text': '<p><strong>There\'s no easy way to find out the memory size of a python object</strong>. One of the problems you may find is that Python objects - like lists and dicts - may have references to other python objects (in this case, what would your size be? The size containing the size of each object or not?). There are some pointers overhead and internal structures related to object types and garbage collection. Finally, some python objects have non-obvious behaviors. For instance, lists reserve space for more objects than they have, most of the time; dicts are even more complicated since they can operate in different ways (they have a different implementation for small number of keys and sometimes they over allocate entries).</p>\n\n<p>There is a <a href="http://code.activestate.com/recipes/544288/" rel="noreferrer">big chunk of code</a> (and an <a href="http://code.activestate.com/recipes/546530/" rel="noreferrer">updated big chunk of code</a>) out there to try to best approximate the size of a python object in memory. </p>\n\n<p>You may also want to check some <a href="http://mail.python.org/pipermail/python-list/2002-March/135223.html" rel="noreferrer">old description about PyObject</a> (the internal C struct that represents virtually all python objects).</p>\n'}, {'answer_id': 19865746, 'author': 'Uzer', 'author_id': 1732607, 'author_profile': 'https://Stackoverflow.com/users/1732607', 'pm_score': 8, 'selected': False, 'text': '<p>Try this:</p>\n<pre><code>sys.getsizeof(object)\n</code></pre>\n<p><a href="https://docs.python.org/3/library/sys.html#sys.getsizeof" rel="noreferrer">getsizeof()</a> Return the size of an object in bytes. It calls the object’s <code>__sizeof__</code> method and adds an additional garbage collector overhead <strong>if</strong> the object is managed by the garbage collector.</p>\n<p><a href="http://code.activestate.com/recipes/577504/" rel="noreferrer">A recursive recipe</a></p>\n'}, {'answer_id': 20906366, 'author': 'rafaelvalle', 'author_id': 3038791, 'author_profile': 'https://Stackoverflow.com/users/3038791', 'pm_score': 3, 'selected': False, 'text': '<p>This must be used with care because an override on the objects __sizeof__ might be misleading.</p>\n\n<p>Using the bregman.suite, some tests with sys.getsizeof output a copy of an array object (data) in an object instance as being bigger than the object itself (mfcc).</p>\n\n<pre><code>&gt;&gt;&gt; mfcc = MelFrequencyCepstrum(filepath, params)\n&gt;&gt;&gt; data = mfcc.X[:]\n&gt;&gt;&gt; sys.getsizeof(mfcc)\n64\n&gt;&gt;&gt; sys.getsizeof(mfcc.X)\n&gt;&gt;&gt;80\n&gt;&gt;&gt; sys.getsizeof(data)\n80\n&gt;&gt;&gt; mfcc\n&lt;bregman.features.MelFrequencyCepstrum object at 0x104ad3e90&gt;\n</code></pre>\n'}, {'answer_id': 24495011, 'author': 'user3791116', 'author_id': 3791116, 'author_profile': 'https://Stackoverflow.com/users/3791116', 'pm_score': 2, 'selected': False, 'text': '<p>For big objects you may use a somewhat crude but effective method:\ncheck how much memory your Python process occupies in the system, then delete the object and compare.</p>\n\n<p>This method has many drawbacks but it will give you a very fast estimate for very big objects.</p>\n'}]
['https://Stackoverflow.com/questions/33978', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3119/']