If, after adhering to all Individuals guidelines, you still have issues with RAM and CPU, then probably it’s because your application is de facto far too hefty, plus a Raspberry Pi isn't the suitable Resource for your task. In cases like this, you might want to have a look at https://servicerobot40617.aioblogs.com/80578451/the-5-second-trick-for-education-robot