GameMaker Language (GML), which is used for scripting in GameMaker Studio, takes care of memory management automatically. It uses a form of garbage collection to free up memory that is no longer needed. Here's how it works:
In GameMaker, you don't usually need to manually allocate or free memory as you would in languages like C or C++. Instead, when you create instances of objects or data structures, GameMaker manages the memory for you.
When an instance of an object is destroyed, either through code using
instance_destroy() or because it goes out of scope at the end of an event or script, GameMaker will automatically free up the memory associated with that instance.
For data structures such as arrays, lists, grids, maps, etc., GameMaker provides functions to explicitly destroy them when they are no longer needed.
If you forget to destroy data structures, they will remain in memory, potentially causing memory leaks. However, if a room ends and there are still data structures that haven't been destroyed, GameMaker will clean them up as part of the room end process, but relying on this is not good practice.
Surfaces and textures also need to be managed. If you create a surface, you should free it when it's no longer needed by using
surface_free(). Similarly, texture assets created dynamically via code should be removed with
GameMaker helps manage memory for you through its garbage collection mechanisms, but there are certain resources that require manual intervention to avoid memory leaks. Properly managing memory by destroying instances and cleaning up data structures, surfaces, and other dynamically allocated resources as soon as they're no longer needed is essential for the performance and stability of your games.