GEM prevents forgetting by combining episodic memory buffers with a constrained optimization routine. It projects gradient updates for new tasks to ensure they do not increase the loss on examples from prior tasks stored in memory, thus preserving old knowledge while learning new information.