This problem began for me a few years ago. I took my Mac to a local authorized repair center instead of the Apple Store because a 27" iMac from 2011 weighs a ton and I didn't want to drag it through a shopping mall to the Apple Store. The local repair place has a parking lot and I can just carry it in.
They tested it and said the graphics card tested bad. So I had it replaced. I forgot what it cost but it wasn't cheap. It worked fine after that but the hard drive tested bad so I had that replaced, too. Between the two repairs, I spent close to $750. At that point, I vowed not to spend one more cent on this Mac.
Lo and behold, a few months later, the problem started again. At first, it wasn't too bad but now it's back to the point where it's freezing my iMac. I want to throw this piece of junk out the window and buy a new iMac but I just lost my job and I can't afford it. I don't know what I'm going to do.
So, there's the correct answer. Throw the iMac out the window, being careful to make sure it doesn't land on anyone.