title: How to solve this memory error - python
body: <p>I have the following code </p>
  
  <pre><code>    def gen_primes():
  
          D = {}  
  
          q = 2  
  
          while True:
              if q not in D:         
                  yield q        
                  D[q * q] = [q]
              else:           
                  for p in D[q]:
                      D.setdefault(p + q, []).append(p)
                  del D[q]
  
              q += 1
  
  
      f = open("primes1.txt","w")
  
      filen = 1
      ran1 = 1
      ran2 = 10000000
  
      k = 1
      for i in gen_primes():
  
          if (k &gt;= ran1) and (k &lt;= ran2):
  
              f.write(str(i) + "\n")
              if k%1000000 == 0:
                  print k
              k = k + 1
          else:
              ran1 = ran2 + 1
              ran2 = ran2 + 10000000
              f.close()
              filen = filen + 1;
              f = open("primes" + str(filen) + ".txt","w")
  
          if k &gt; 100000000:           
              break
      f.close()
  </code></pre>
  
  <p>The prime generation algorithm is taken from <a href="https://stackoverflow.com/questions/567222/simple-prime-generator-in-python">Simple Prime Generator in Python</a></p>
  
  <p>This program is giving memory error</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Python25\Projects\test.py", line 43, in &lt;module&gt;
      for i in gen_primes():
    File "C:\Python25\Projects\test.py", line 30, in gen_primes
      D.setdefault(p + q, []).append(p)
  MemoryError
  </code></pre>
  
  <p>I am trying to store consecutive 10,000,000 primes in one file.</p>