Legacy GM Memory Leak

B

Bird Man

Guest
So I am making a turn based rts game in space. The following script is what I use to draw the planets and there is a memory leak somewhere in it:
Code:
//dr_planet(ground,ocean,poles,atmos,dir,rot_spd,size);
var col_g = argument0;
var col_o = argument1;
var col_p = argument2;
var col_a = argument3;

var dir = argument4;
var rot = argument5;

var size =argument6;

var pc = surface_create(64,64);                                                     //
var pf = surface_create(64,64);                                                     //

if surface_exists(pc) and surface_exists(pf) {
    surface_set_target(pc);
    draw_sprite_ext(spr_ground,0,ix,0,1,1,0,col_g,1);
    draw_sprite_ext(spr_ground,0,ix-128,0,1,1,0,col_g,1);
    draw_sprite_ext(spr_amap,0,ix,iy,1,1,0,col_o,1);
    draw_sprite_ext(spr_amap,0,ix-128,iy,1,1,0,col_o,1);
    draw_sprite_ext(spr_poles,0,64/2,-54,.5,.5,45*ix/128,col_p,1);
    draw_sprite_ext(spr_poles,0,64/2,64+54,.5,.5,-45*ix/128,col_p,1);
 
    b = sprite_create_from_surface(pc,0,0,64,64,false,false,32,32);                 //
 
    surface_reset_target();
 
    var m = sprite_duplicate(spr_mask);                                             //
    sprite_set_alpha_from_sprite(b,m);
 
    surface_set_target(pf);
    draw_sprite(b,0,32,32);
    draw_sprite_ext(spr_shadow,0,32,32,1.1,1.1,0,c_white,1);
    surface_reset_target();
 
    p = sprite_create_from_surface(pf,0,0,64,64,false,false,32,32);                 //
 
    draw_sprite_ext(p,0,x,y,size/64,size/64,image_angle,c_white,1);
    draw_sprite_ext(spr_atmos,0,x,y,size/85.33,size/85.33,image_angle,col_a,1);
    ix += rot;
    image_angle += .01;
    if ix>128 {ix -= 128;}
         
    sprite_delete(m);
    sprite_delete(b);
    sprite_delete(p);
}
surface_free(pf);
surface_free(pc);
You can see that I delete all the newly created sprites and free the surfaces at the end so I don't know what is the cause.
If anyone could help it would be greatly appreciated.
 

TsukaYuriko

☄️
Forum Staff
Moderator
How are you determining that a memory leak exists? Is the game actually crashing because it runs out of memory, or does the memory usage just not decrease upon freeing the resources? If it's the latter, it might be important to know that freed memory does not necessarily become unreserved instantly.

Try observing the memory usage of your game for an extended period of time after calling the code you posted. I suspect that it will eventually drop... it might just take a while.
 

Tizzio

Member
Do you call this script every frame? BAD IDEA
Use a shader to achieve the effect you want combining the surfaces you have.
 

Yal

🐧 *penguin noises*
GMC Elder
Just because you free up resources doesn't mean the program will RETURN the memory they used, it'll simply have it free to use for other things next time it needs to create more dynamic resources. I'm not sure how GM handles memory allocation, but I do know what memory allocation in Windows takes an unknown amount of time (unlike RTOSes like uC/OS2 where you predefine blocks of memory for allocation) so it would make sense that GM would use them with care to not slow down the game unnecessarily.
 
J

jackhigh24

Guest
ye i had this problem to where i was creating a sprite from the application suface and deleting it after using it, this was for a screenshot so the next screenshot would have the exact same name as id deleted the first and so on, the memory usage just keeps going up each time you create a sprite from the suface and never goes back down until you quite the game, so maybe a bug but as it is only a small increase then it wont course a problem in my game unless someone takes say about 2000 screen shots before quitting the game.
 
B

Bird Man

Guest
Thx for all the replies.

@TsukaYuriko When I run with the debugger is see that the memory increases by about 2K/s.
@Tizzio Yes this is run every frame :/, the idea is to create a planet with a surface that is rotating:

show.png

I have never used shaders and have no idea how to use them.
 
R

Robert

Guest
Ive noticed this happens on a couple things actually. I had a huge headache with path_add and it causing a memory leak despite the added paths being removed. I think adding resources like this dynamically will inevitably lead to slow downs. Surfaces no, but sprites and paths yes.
 

Andrey

Member
It may be necessary to look for other ways to simulate the planet? For example, using texturing function 3d3 ?
 
P

Paolo Mazzon

Guest
@TsukaYuriko When I run with the debugger is see that the memory increases by about 2K/s.
Doesn't the debugger show how much memory your program has gone through total? (In which case, it would always be increasing if you're allocating and deallocating memory all the time) I'd take a look at it in task manager, see it it goes up there.
 
R

Robert

Guest
Doesn't the debugger show how much memory your program has gone through total? (In which case, it would always be increasing if you're allocating and deallocating memory all the time) I'd take a look at it in task manager, see it it goes up there.
No, it shows the current used memory.
 
J

jackhigh24

Guest
yes what he is saying is true as i said before iv seen this the same on mine and had it checkd with a few other to see and it did it for them too, on mine i was grabbing the full screen to sprite and every time it increased 23.6 kb never went done until a shut the game down and restarted, so maybe it would be better to save to a surface as people say that works without accumulating in memory.
 
Top