import resource import traceback def limit_memory(maxsize): soft, hard = resource.getrlimit(resource.RLIMIT_AS) resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard)) if __name__=="__main__": limit_memory(20) try: list_a = [] _i = 0 while True: _i += 1 print(_i) list_a.append("aaaaaaaaaaaaaaaaa") except Exception as e: print("Memory error 1") traceback.print_exc()