title: calculating the real size of a python string
body: <p>First of all this is my computer Spec :</p>
  
  <p>Memory - <a href="https://gist.github.com/vyscond/6425304" rel="nofollow">https://gist.github.com/vyscond/6425304</a></p>
  
  <p>CPU - <a href="https://gist.github.com/vyscond/6425322" rel="nofollow">https://gist.github.com/vyscond/6425322</a></p>
  
  <p>So this morning I've tested the following 2 code snippets:</p>
  
  <p>code A</p>
  
  <pre><code>a = 'a' * 1000000000
  </code></pre>
  
  <p>and code B</p>
  
  <pre><code>a = 'a' * 10000000000
  </code></pre>
  
  <p>The code A works fine. But the code B give me some error message :</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  MemoryError
  </code></pre>
  
  <p>So I started a researching about method to measuring the size of data on python.</p>
  
  <p>The first thing I've found is the classic built-in function <code>len()</code>.</p>
  
  <p>for code A function <code>len()</code> returned the value <code>1000000000</code>, but for code B the same memory error was returned.</p>
  
  <p>After this I decided to get more precision on this tests. So I've found a function from the <code>sys</code> module called <code>getsizeof()</code>. With this function I made the same test on code A:</p>
  
  <pre><code>sys.getsizeof( 'a' * 1000000000 )
  </code></pre>
  
  <p>the result return is <code>1000000037</code> (in bytes)</p>
  
  <ul>
  <li>question 1 - which means <code>0.9313226090744</code> gigabytes?</li>
  </ul>
  
  <p>So I checked the amount of bytes of a string with a single character <code>'a'</code></p>
  
  <pre><code>sys.getsizeof( 'a' )
  </code></pre>
  
  <p>the result return is <code>38</code> (in bytes)</p>
  
  <ul>
  <li><p>question 02 - which means if we need a string composed of <em>1000000000</em> character <code>'a'</code> this will result in 38 * 1000000000 = 38.000.000.000 bytes?</p></li>
  <li><p>question 03 - which means we need a 35.390257835388 gigabytes to hold a string like this?</p></li>
  </ul>
  
  <p>I would like to know where is the error in this reasoning! Because this not any sense to me '-'</p>