1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 Memory Leak

Discussion in 'Programming' started by Bird Man, Jun 29, 2016.

  1. Bird Man

    Bird Man Member

    Joined:
    Jun 29, 2016
    Posts:
    2
    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.
     
  2. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,538
    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.
     
  3. Tizzio

    Tizzio Member

    Joined:
    Jun 20, 2016
    Posts:
    17
    Do you call this script every frame? BAD IDEA
    Use a shader to achieve the effect you want combining the surfaces you have.
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,460
    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.
     
  5. jackhigh24

    jackhigh24 Member

    Joined:
    Jun 21, 2016
    Posts:
    604
    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.
     
  6. Bird Man

    Bird Man Member

    Joined:
    Jun 29, 2016
    Posts:
    2
    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.
     
  7. Robert

    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.
     
  8. Andrey

    Andrey Member

    Joined:
    Jun 21, 2016
    Posts:
    273
    It may be necessary to look for other ways to simulate the planet? For example, using texturing function 3d3 ?
     
  9. Paolo Mazzon

    Paolo Mazzon 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.
     
  10. Robert

    Robert Guest

    No, it shows the current used memory.
     
  11. jackhigh24

    jackhigh24 Member

    Joined:
    Jun 21, 2016
    Posts:
    604
    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.
     
  12. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
  13. Andrey

    Andrey Member

    Joined:
    Jun 21, 2016
    Posts:
    273

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice