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:
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)
I'm using other functions which I call from the main
function.
What am I doing wrong?
Thanks in advance.
PD: I already searched about this and found the code I've put but it's still not working...
Limiting CPU and Memory Usage Resources like CPU, memory utilised by our Python program can be controlled using the resource library. To get the processor time (in seconds) that a process can use, we can use the resource. getrlimit() method. It returns the current soft and hard limit of the resource.
to clear Memory in Python just use del. By using del you can clear the memory which is you are not wanting. By using del you can clear variables, arrays, lists etc.
Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager.
Ok so I've made some research and found a function to get the memory from Linux systems here: Determine free RAM in Python and I modified it a bit to get just the free memory available and set the maximum memory available as its half.
Code:
def memory_limit(): soft, hard = resource.getrlimit(resource.RLIMIT_AS) resource.setrlimit(resource.RLIMIT_AS, (get_memory() * 1024 / 2, hard)) def get_memory(): with open('/proc/meminfo', 'r') as mem: free_memory = 0 for i in mem: sline = i.split() if str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'): free_memory += int(sline[1]) return free_memory if __name__ == '__main__': memory_limit() # Limitates maximun memory usage to half try: main() except MemoryError: sys.stderr.write('\n\nERROR: Memory Exception\n') sys.exit(1)
The line to set it to the half is resource.setrlimit(resource.RLIMIT_AS, (get_memory() * 1024 / 2, hard))
where get_memory() * 1024 / 2
sets it to the half (it's in bytes).
Hope this can help others in future with the same matter! =)
I modify the answer of @Ulises CT. Because I think to change too much original function is not so good, so I turn it to a decorator. I hope it helps.
import resource import platform import sys def memory_limit(percentage: float): """ 只在linux操作系统起作用 """ if platform.system() != "Linux": print('Only works on linux!') return soft, hard = resource.getrlimit(resource.RLIMIT_AS) resource.setrlimit(resource.RLIMIT_AS, (get_memory() * 1024 * percentage, hard)) def get_memory(): with open('/proc/meminfo', 'r') as mem: free_memory = 0 for i in mem: sline = i.split() if str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'): free_memory += int(sline[1]) return free_memory def memory(percentage=0.8): def decorator(function): def wrapper(*args, **kwargs): memory_limit(percentage) try: return function(*args, **kwargs) except MemoryError: mem = get_memory() / 1024 /1024 print('Remain: %.2f GB' % mem) sys.stderr.write('\n\nERROR: Memory Exception\n') sys.exit(1) return wrapper return decorator @memory(percentage=0.8) def main(): print('My memory is limited to 80%.')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With