My 2017 iMac running macOS 10.12.6 has a problem that occurs on a roughly weekly basis where the entire system will gradually freeze. The symptoms are that an application will lock up with the spinning pinwheel of death, and as I try to force quit it or switch to other applications, they will eventually start locking up too in the same manner. This includes apps like Finder, Dock, Spotlight, SystemUIServer, and so on. Eventually the entire system is completely frozen. At this point I can't use the menu bar, open a terminal window or SSH into the system, so I have no choice but to do a hard reboot.
This same issue would happen on my previous mac, and came back even though I set up my current mac completely from scratch (rather than restoring a Time Machine backup or something similar). So that leads me to believe that a piece of software or hardware that I'm using is the culprit, and not a physical defect in the mac itself.
However, I have no idea how to debug a crash like this. Since the system never kernel panics and no app ever actually fully crashes, I have no panic or crash logs to look into.
What can I do to try and debug this problem? Is there any kind of logging that could point to why everything starts freezing?