Having correctly configured swap/zram/zswap and a correctly working oom-daemon can help immensely. The in-kernel oom is uhm kinda bad for most desktop use cases not usable by default. You can try playing around with these https://wiki.archlinux.org/title/Improving_performance#Impro... most linux distributions come with some form of this out of the box (ubuntu tries systemd-oomd with custom settings and earlyoom is enabled by default on fedora afaik).
If you are lazy you can try nohangd which is nice because it warns you with a notification before taking action.