Reduce Ram Usage / CPU Usage in Python programs

I am trying to get a step by step process on how to add a limit / reduce memory usage to a specific python program by python script or bash shell. Any ideas would greatly be appreciated.

Issue: I have a python program that consumer up to 80% of my ram usage and I have a 8GB Ram and 10G Swap.

cpulimit is ok but when ever there is a reboot or program restart the PID changes. I am also looking for steps using a Linux / Debian platform.

I suspect this is more of an Operating System issue than Programming Language issue.

You may want to try using a virtual machine, or maybe a container.

1 Like

Maybe make a cgroup and then run the program under said cgroup via cgexec?

This stackexchange post seems to cover that: Limit memory usage for a single Linux process - Unix & Linux Stack Exchange.

Though one thing to consider is possibly refactoring the code to use less memory (or other resources.). You can put limits, but that can result in the code not functioning properly if it actual needs those resources to run.