system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: What happens when a program freezes/stops responding?
gpt
There are many different ways in which a computer can be said to "freeze" and there can be many different reasons, but it's generally down to 2 things.

1. A poorly programmed application.

  Usually when we say that a program has frozen, we mean that the user interface has become unresponsive. This may also refer to the desktop or shell, which is itself just a program.

  When you move the mouse over the window of an application, click somewhere within it or press a key while the window has focus, these are called *events*, and the application is informed of these events. The application will then have a central event handler whose job is to decide what to do about certain events. For example, if the window receives a mouse move event, it will work out what is underneath the mouse cursor (e.g. a button) and then pass the event to that component. That component may then change some internal state so that it draws in a different way (e.g. a button might now draw with a glow effect to show that it's ready to be pressed).

  In order for a user interface to remain responsive, it must handle events in a timely manner. If it takes too long to deal with events, then the events might start to arrive faster than they are dealt with and begin to queue up. This can lead to noticeable delays, which gives the appearance of a laggy and unresponsive application, and if it gets bad enough, the application may appear to have completely frozen. One of the important events from the operating system is one telling the window that it must redraw itself. When these events aren't handled, the window will go blank, or display old images (e.g. of the window that used to be on top of it).

  The most common cause of this is down to poor programming, when the program starts some work that may take a long time (e.g. opening a file or connecting to a server over the Internet) while responding to an event. This lengthy work blocks the processing of any further events until it's completed. You'll most often see this when a poorly programmed application tries to connect to a server that's down or where the Internet connection is extremely slow; the entire application will freeze for several seconds before displaying an error. The correct thing to do is to schedule the work to be completed in the background (in a separate thread of execution), freeing up the application to continue to respond to events.

2. High resource contention. 

  When many applications becomes unresponsive, or everything does, rather than just a single application, this is usually because the system is lacking some resource, the two most common of which are CPU time and memory. For example, if the system is running out of memory, then it has to spend a significant amount of time swapping data between memory and the paging file on disk (which is very slow in comparison).