• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Clean up memory explanation.

A

Ahsen

Guest
Hello. I'm new to gamemaker and I've been reading a lot about it and I came up with some important stuff. I read about it but I'd like ask if somebody could explain to me about how gamemaker deals with it.

As far I understand, every object on destroy is already removed from memory right? And about data structures every single one of them have to be destroyd too if I'm not using, correct? But if I need a big ds for the entire time until the players leaves a level that could last for 1 hour? Since I'm doing a randomly generated map, all the map data have to be in my grid so that's why I'm concerned about it.
One last thing: accordingly to manual, the variable that holds the ds have to be set to -1 after I destroied my ds. Why? This means that I have to take care discarting it from memory for every single variable too?

Thanks!

edit: Also, apart from global, variables persist between rooms? if so how should I manage this?

Basically I'm confused about how should I manage memory in gms.
 
Last edited by a moderator:

Nocturne

Friendly Tyrant
Forum Staff
Admin
As far I understand, every object on destroy is already removed from memory right?
Yes

And about data structures every single one of them have to be destroyd too if I'm not using, correct?
Yes (more or less... you can store a ds in a global variable for the whole game and never destroy it, but in general any resource created at runtime will need cleaned up when no longer required).

But if I need a big ds for the entire time until the players leaves a level that could last for 1 hour?
Then you make a big DS list! The time you use it doesn't matter, only that you clean up afterwards.

Since I'm doing a randomly generated map, all the map data have to be in my grid so that's why I'm concerned about it.
That's not an issue as I'll explain in a moment. My game Skein uses a DS grid for everything in the game from spawning to collisions and the grid is created on Room start and destroyed on Room end. So, this is pretty much what grids are for. :)

One last thing: accordingly to manual, the variable that holds the ds have to be set to -1 after I destroied my ds. Why? This means that I have to take care discarting it from memory for every single variable too?
Okay, this is for a very specific reason... DS id values are integers, and they get RE-USED, so if you have a ds map with index 2 and then destroy it but later want to check it or create another, how do you check in your code that the first ds map was destroyed and no longer exists before making the second? By explicitly setting the id value to -1, the check is simple... if the variable for the DS is -1, then it doesn't exist... basically it's just to help with complex code that may have multiple data structures since DS ids are integer values and are also re-used.

edit: Also, apart from global, variables persist between rooms? if so how should I manage this?
No, local and instance variables do NOT persist between rooms unless the instance that uses them has been flagged as persistent. The only things that are "global" and available in all rooms are enums, macros, and global variables. See here for more information: http://docs2.yoyogames.com/index.html?page=source/_build/3_scripting/3_gml_overview/6_scope.html

Basically I'm confused about how should I manage memory in gms.
Okay, so, two main rules to follow:

1) If you create something at runtime that isn't in your resource tree, then it will need cleaned up when you no longer need it. EG: data-structures, buffers, particles.
2) You can use a global variable to store a created asset if you need to use it across various rooms.

If you have further questions just ask!
 

TheouAegis

Member
It amazes me how many people are so confused by this...

You need to destroy resources and data structures in order to avoid memory leaks. Leaving data structures in your game indefinitely is not a memory leak. You could create 1000 unique data structures, never delete them, and not have a memory leak. You are just allocating memory in that case. Creating a data structure you use once and never again but don't delete is just wasted memory. Creating a data structure and then creating another one that does the exact same thing without deleting the first is a memory leak.

The first tip I can give is avoid using room_restart and game_restart. Both functions cause memory leaks anyway. My second tip is to create all your data structures at the very start of the game from a room you can never visit again.
 
A

Ahsen

Guest
@Nocturne Thanks for your time! I got almost everything but I still have some concept confusion in my head. Lets say I create a grid and store its ID to variable dsA, do whatever I want with it and then I destroy this grid but I don't set dsA to -1. After sometime I create another grid(that happens to use the same index) and store it to variable dsB and call it in a routine. How the index stored in dsA would affect dsB?
@TheouAegis Thanks! Well, I have like one week of gamemaker so I think it's normal to have stupid doubts ^^. So you're saying that having an empty room initializing all my ds that will be used throught all the game is a good practice?
 

TheouAegis

Member
In general, yes. If you're going to be creating and destroying data structures over and over and over, then it's best to just create them once and leave them in the memory if you can. Likewise, you should recycle any data structures that you use once and discard. For example, suppose you want to generate a random set of options so you populate a ds_list with all the options and then shuffle it, pick the first n options and then destroy the ds_list. Well, instead do something like

global.one_off = ds_list_create();

in the beginning of the game and then any time you need a one-off list, just use ds_list_clear(global.one_off) first and then do everything as normal; when you're done with the list, just leave it alone until the next time it needs to get used.


As for your question to Nocturne, dsA and dsB are unrelated but would only conflict if you still tried to pass dsA to functions. If you don't set dsA to -1, then ds_exists(dsA,ds_type_list) would still be true, for example, even though you deleted the list assigned to dsA already, because now dsA will be pointing to dsB's list.
 
Last edited:
Top