title: Limit RAM usage to python program
body: <p>I'm trying to limit the RAM usage from a Python program to half so it doesn't totally freezes when all the RAM is used, for this I'm using the following code which is not working and my laptop is still freezing:</p>
  
  <pre><code>import sys
  import resource
  
  def memory_limit():
      rsrc = resource.RLIMIT_DATA
      soft, hard = resource.getrlimit(rsrc)
      soft /= 2
      resource.setrlimit(rsrc, (soft, hard))
  
  if __name__ == '__main__':
      memory_limit() # Limitates maximun memory usage to half
      try:
          main()
      except MemoryError:
          sys.stderr.write('MAXIMUM MEMORY EXCEEDED')
          sys.exit(-1)
  </code></pre>
  
  <p>I'm using other functions which I call from the <code>main</code> function.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks in advance.</p>
  
  <p>PD: I already searched about this and found the code I've put but it's still not working...</p>