SDL Memory usage

I made a very small program and it is reporting a large amount of used memory:

$ ps -u -p 67182
PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
67182  0.1  0.0 423288 33324 pts/1    Sl+  20:40   0:00 ./small

Something around 430MB. Valgrind reports 900MB used

total heap usage: 25,057 allocs, 23,080 frees, 906,658,522 bytes allocated

Is that normal?

The source code is here:

#include <SDL2/SDL.h>

int main(void) { 
    SDL_bool quit = SDL_FALSE;
    int retval;
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window= SDL_CreateWindow("Gamepads and viewports",
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            800,
            600,
            SDL_WINDOW_SHOWN);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_SetHint ("SDL_RENDER_BATCHING", "1");
    retval = SDL_SetRenderDrawColor(renderer, 10, 10, 10, 255);
    retval = SDL_RenderClear(renderer);
    SDL_Event *event = SDL_malloc( sizeof (*event));
    char *pref_path = SDL_GetPrefPath("inc", "gamepad");
    char *base_path = SDL_GetBasePath();
    size_t count = 0;
    while(quit == SDL_FALSE) {
        while(SDL_PollEvent(event) > 0 )  {
            switch(event->type)  {
                case SDL_QUIT: quit = SDL_TRUE; break;
                case SDL_KEYDOWN: switch(event->key.keysym.sym)  {
                    case SDLK_ESCAPE: quit = SDL_TRUE; break;
                }
            }
        }
        SDL_RenderClear(renderer);
        SDL_Log("Frame Count: %8zu\r", count++);
        SDL_Delay(200);
    }
    SDL_free(pref_path);
    SDL_free(base_path);
    SDL_free(event);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    return 0;
}

VSZ is just the reserved virtual memory space, RSS is the actual memory used, so around 33MB - still seems like a lot for a program that does almost nothing, but maybe it’s buffers for the renderer (possibly even in the GPU driver’s userspace part)?
I think having lots of allocs after some time is not surprising, IIRC SDLs internal event queue uses malloc() for each event - and most of those allocations seem to be freed again.
It’s weird though that there’s around 2000 allocs without corresponding free - does that change if you call SDL_Quit(); at the end?

BTW: You don’t have to allocate your event on the heap, you can as well do

SDL_Event event;
while(SDL_PollEvent(&event)) {
    switch(event.type) {
    // ...
    }
}

(But of course that one malloc() doesn’t make a difference for your test, just a general thing :slight_smile: )

Thanks for the reply Daniel,

Just smaller difference between allocs and frees. Memory usage is almost the same.

total heap usage: 25,134 allocs, 24,128 frees, 912,017,315 bytes allocated

I know that, but is there any advantage? Doing on heap I can allocate in some initialization function and return just a pointer instead using globals (that I’m just using here for sake of simplicity).

I’m not sure where the remaining about 1000 allocs that aren’t freed come from, maybe the libGL or some other system lib allocates things that aren’t freed (because they’re only allocated once and are freed automatically when the process ends anyway).

Doing on heap I can allocate in some initialization function and return just a pointer instead using globals (that I’m just using here for sake of simplicity).

That doesn’t make sense.
If you need them globally, you can as well use a global SDL_Event instead of a global SDL_Event* (but even then, what use is having only the last polled event globally?)
If you only need them in the function that calls SDL_PollEvent() (which is usually the case), just make it a local variable in that function as I suggested, so it gets allocated on the stack, no need to allocate it on the heap.

OMG. That is good to know. I will run some experiments here.