Sounds to me like a memory leak is exhausting your available memory.
You might want to use the Processes and Performance tabs in TaskManager to see if you can identify the culprit.
You use the word "randomly" twice...
That would suggest that you are getting the error, even when the machine is idle??
Which again suggests that something (program, process, service, etc.) is running, perhaps in the background.
Make sure that you are fully updated to the latest available version of Windows 11