Thread: SDL taking too many resources

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    SDL taking too many resources

    I am currently writing a game in Microsoft Visual C++ 6 using SDL 1.2.4.0, but I have a problem. When the game starts, it takes up all my available ram (About 200Mb). This is not too much of a problem though. The real problem is that it then starts using virtual memory. I have about 1.1Gb of Virtual memory, and initially it uses only a couple of megs. Then it slowly uses more and more. After about two minutes, it is using 1Gb. Then soon after, when all the ram is used, it crashes.
    Any advice?

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure the problem is with SDL, and not with your part of the code? Have you used some sort of memory profiling tool to see where the memory is allocated. As far as I know [from Linux, admittedly], SDL is not a very large product as such - but that may differ depending on what you do.

    How large is the executable file itself?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    I'm not sure where the problem is. The menu in my game works fine. I think its the textures I'm loading. I think the executable is about 132Kb.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Sounds like you have a memory leak.

    Do you free memory and release resources?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    using SDL 1.2.4.0
    That's ancient. I have SDL 1.2.11. Nevertheless, I seriously doubt the problem is with the SDL. It's almost certainly with your code. Would you consider posting your source code? Perhaps someone might be able to spot a memory leak in your code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Are you sure that you are not loading images or other media inside your main loop? I did that once when I started programming and It quickly ate up all the RAM and worked its way into the paging file.

    If the leak eats all the RAM fast then its going to have to be something that uses a lot of space like images or audio.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Available RAM as in video RAM or volatile RAM? If all you have is 200MB you have more problems than just this.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    I'm talking about my main system ram. I have about 1200 lines of messy code, so I think thats a bit too much to post. The problem is with my code, not with sdl. I do free resources, but not memory. How do I free memory?

    I'm not loading images in my main loop, but I do have a seperate loop for the map, and I think thats where the problem is. Has anyone got any suggestsions for things I can look for?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There should be an equivalen free/delete for every malloc/new in your code. If you don't have that, then you are leaking memory. There are tools available to detect memory leaks, for example in Microsofts compiler Visual C/C++ compilers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    I never have used and have no idea how to use malloc. Never really needed it. But I've decided to ditch sdl in C for directx in c#. I wanted to go the directx route in the beggining, but having dialup I couldnt download the sdk. But I found that the files for managed directx are in my windows directory. Sorry for wasting all you guys' time, but thanks for all the help!

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    So basically if you think you need say 1000 particles in your game you're going:
    Code:
    struct particle p[1500];
    So on and so forth, no wonder why you're running out of memory. You should only be asking for how much you need, not "might need".

    Remember you don't have to use malloc() to be allocating memory, each function in SDL has a corresponding 'free' function. SDL_FreeSurface() for example.

  12. #12
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    I never have used and have no idea how to use malloc. Never really needed it. But I've decided to ditch sdl in C for directx in c#.
    No offense but changing languages and SDKs ain't gonna fix the fact that you have been using bad coding practices and allowing memory leaks to occur (although C# will be nice to you and clean up for you).

    If you have never used malloc() and free() and/or new and delete then it is pretty much a guarantee that you had huge memory leaks going on. You need to free memory that you are finished using. I suggest learning more about pointers and memory allocation, as the knowledge of it is invaluable in the programming world....EVEN if you typically use some language that gives you a garbage collection feature.
    My Website

    "Circular logic is good because it is."

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Ok thanks for the tips. I dont really like pointers, but I need to learn how to use them properly.
    BTW I was using SDL_FreeSurface() and FreeFont(), but I wasnt freeing anything else.

  14. #14
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Considering that so far you only have a very simple game you should not have a problem with freeing memory unless you are loading more image data than can be held in ram. So far you probably don't need that much RAM anyway unless you are using tons of large images.

    You are loading each image only once right? IE: you only need a reference to a sprites image; you don't want to load a copy of the image for each sprite or tile.

    TBH theres not really much people here can do to help you except guess where you could be going wrong unless you post the code. 1200 lines is not too much to go through if you post it in an attachment, and you have it presentable (IE: indented).

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't personally have time to browse through 1200 lines of code and even if I did that would feel too much like being at work and not at home.

    First problem: You are using MSVC 6.
    Second problem: You don't know how to manage memory.
    Third problem: Your project requires a skill set that you do not currently have. Try something simpler.

    Learn the fundamentals of the language and then code your game. I'm not saying you have to be super C++ man to program a game and learning while you code is fine, but you should at least grasp the concepts. With the internet so widely available you can google your exact problem and get tons of hits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  2. SDL and MinGW Studio
    By Vicious in forum Tech Board
    Replies: 0
    Last Post: 07-30-2004, 09:59 PM
  3. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  4. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM