How to Flush Memory in GM Project

C

ChaosX2

Guest
Hi Everyone,

I'm working with ds maps and list for saving objects and while I was messing around, I believe at one point I caused a memory leak before getting the code back to normal to try another approach. With that said, the normal working code will not produce the JSON list, but if I start a new project and copy/paste my code exactly, it works.

The only other thing I can think of is that when I caused the leak a while back, GameMaker is retaining the memory of the old variables and it isn't creating the map and list properly because indexed data structures could potentially use the same index number. I tried using ds_map_clear and ds_list_clear but it didn't work.

So I'm wondering if anyone knows of a way to simply flush the project of allocated memory, so it starts fresh upon playing your game.

Thanks in advance!
 

RangerX

Member
Everything you basically create at run time must be deleted.
That's why in the manual (read it!) you have those functions:

ds___ delete/clear
sprite delete
background delete
surface delete
etc.

It doesn't prevent any memory leak to happen though. Let's say you create a background everytime you press pause but you assign it to the same variable. If you didn't delete the previous sprite that was indexed in that variable you will loose its index forever.
 

Paskaler

Member
I believe that the OS should reclaim all of GameMaker's memory once you close the game. Still, you should use ds_list_destroy and ds_map_destroy in appropriate places in your code(unless they're global and you need them for the entirety of the game). If you are storing their indices inside an object variable destroy them in the Destroy or Clean Up event(the latter is in GMS 2). Do note, that the Destroy event won't get called when you switch rooms(the objects will be destroyed, tough, so you'll lose the reference you need to destroy them). You only have to worry about the Destroy event in GMS 1(use Clean Up in 2). Personally, I use this script for room transitions:

Code:
room_goto_safe(rm_index)

with all if not persistent then instance_destroy();

room_goto(argument0);
 
C

ChaosX2

Guest
Thanks for the response guys. Happy to say that I use the ds_delete function when appropriate. I think it was just that one time I may have forgotten to do it with a map yesterday.

My impression, for example, is this.

map_A is created and assigned an index of say 1.
map_B is created and assigned an index of 2.

map_B creates a memory leak. I catch it and fix it by deleting the variable all-together.

I create map_C but instead of it receiving an index of 3, it's receiving an index of 2 because I erased the map_B variable. But in memory, it still believes map_B exist and does not reference map_c appropriately.

I don't know if I'm explaining it right or if this is even a thing. I have no idea why the code won't work in my main project but works in others. This is only a guess, so I know I can be completely wrong about this...
 

Paskaler

Member
I can't tell if it is clear: the *_destroy and *_delete are two different functions. The first frees memory, the second deletes an element.
 

RangerX

Member
mapA is created and its index is saved in variable 1
mapB is created and its index is saved in variable 1 --- now you have a memory leak because you lost the index of mapA

-----------------

mapA is created and its index is saved in variable 1
you "map_delete(variable1)"
mapB is created and its index is saved in variable 1 ---- no memory leak


(function map_delete doesn't exist, its just to make a point)
 
C

ChaosX2

Guest
Sorry, I meant ds_map_destroy() and ds_list _destroy() respectively.

The clear cache button doesn't fix the issue. I've tried it several times (and just now to be sure).

Thanks for the example RangerX. I'm going to try and recreate the names of the data structures I no longer use and run the destroy function on them to see if it helps any.
 
Last edited by a moderator:

Dog Slobber

Member
So I'm wondering if anyone knows of a way to simply flush the project of allocated memory, so it starts fresh upon playing your game.
Are you suggesting that your game is retaining memory or has some kind of memory leak from previous runs of the game.

If you are, I can assure you, this is not the case.. Every time you run your game it starts fresh.
 
C

ChaosX2

Guest
Are you suggesting that your game is retaining memory or has some kind of memory leak from previous runs of the game.

If you are, I can assure you, this is not the case.. Every time you run your game it starts fresh.
Thanks for the reply and assurance. I did a bit of debugging to cycle through the keys in my map and sure enough, that's the problem...

It's not adding the keys I want to the map and it's adding its own keys. More specifically, it's adding the states of my game state machine controller. I have no idea why this is happening when I have no such code to say add these as keys to the map. So I'm working on figuring out what's going on with that.

[Edit]
After starting up the game, I cleared the map upon a key press and added a key/value to the map upon another key press. The code saves it properly. So something is happening as soon as I start the game where it's not populating the map with the data I want and I have no idea why that is as of yet.
 
Last edited by a moderator:
Top