What Is A Memory Leak?

Discussion in 'TSW General Discussion' started by bakedpotatos.jm, Oct 6, 2024.

Thread Status:
Not open for further replies.
  1. bakedpotatos.jm

    bakedpotatos.jm Well-Known Member

    Joined:
    Nov 10, 2020
    Messages:
    1,485
    Likes Received:
    2,653
    I'm not a very tech savy person, just curious on what exactly is a memory leak and how would you fix it?

    My thought is a memory leak is when some feature is using way more memory than it needs to.

    But as i said I'm useless with technology so I may be way off.

    So just looking for clarity from people who know.

    Thanks.
     
  2. 10 seconds of googling yielded the following..

    "
    A memory leak in a game occurs when a program allocates memory for an object or resource but fails to release it when it's no longer needed. This can lead to performance issues, crashes, and user frustration.

    Here are some signs of a memory leak:

    • The amount of memory used increases over time

    • The memory use is not typical of the programs being run or the current load on the system

    • The computer becomes less responsive or is entirely stuck
    Memory leaks are a common problem in modern software and can be caused by programming oversights. They can be diagnosed by a programmer with access to the program's source code.

    Memory leaks are not permanent, and there is no physical defect."
     
  3. DTG Matt

    DTG Matt Executive Producer Staff Member

    Joined:
    Nov 17, 2016
    Messages:
    3,295
    Likes Received:
    18,497
    Memory leaks are where memory grows over time, there can be numerous causes.

    The main one though is where memory is not free'd up entirely - so some memory is allocated, only some of it is free'd up. Over time, you're losing more and more ram to the leak and you crash.

    Memory management is actually an incredibly complex topic, keeping track of all the memory that's allocated and ensuring it's all free'd up in the right order at the right time can be tricky in a big complex system. If you free the ram up to soon, the game crashes because it steps on things it shouldn't anymore - if you try to free it up too late, it could be that it ends up building up over time and you run out.

    Modern systems like Java and .NET tend to keep track of things via their references - so if you have a block of memory X, and three objects have references to it, then those three objects go away, it detects that there's nothing left with a reference to X and free's it up automatically. Unfortunately, the reality is that you can very quickly end up with all kinds of causes where things don't have zero references and *still* end up with memory leaks, just of a different cause. I'd argue that these systems actually make it *easier* to have memory leaks because it does fool anyone but the expert engineer into thinking its being done for you, whereas the expert doesn't trust magic and tracks it all themselves anyway.

    Imagine you're driving down the route and scenery is loading as you go, certain types of scenery are being looked for - maybe point levers, things that have animation - and then a reference to them is grabbed so that they can have their animations controlled based on whether they are too far away, not visible etc. When the area they're on goes out of the world, if there isn't specific handling for it, that reference *could* keep all those things in memory even though all the trees and buildings were unloaded, so you need to track for that and make sure that the references are removed in a timely manner to allow them to be freed up correctly.

    Another example is where you are just using too much RAM, it's not a leak so much, but for example over time in a scenario some assets are loaded and just kept in ram because they're potentially often used - if there isn't some careful tracking of it, and it doesn't correctly realise that it should be offloading some things, that buffer can just grow to exceed ram and cause an out of memory.

    There's lots of causes, it varies case by case. Don't think any of the above apply to the game, but, in case anyone's thinking "well those are just stupid why even make that mistake" - it's never as simple or obvious as the above.

    Not sure the sarcasm is really constructive, but okay.

    Matt.
     
    • Like Like x 13
    • Helpful Helpful x 5
  4. It wasnt sarcasm.
    It took me 10 seconds of googling. The implication being that a little effort results in a desired outcome.
    Not sure assumptions about your customers are really constructive either. Its certainly not a good way of representing the brand.
     
    Last edited by a moderator: Oct 6, 2024
    • Like Like x 6
  5. aeronautic237

    aeronautic237 Well-Known Member

    Joined:
    Sep 1, 2022
    Messages:
    3,202
    Likes Received:
    3,058
    Eh, I probably wouldn't knock the OP for asking a question even if it was a simple one. Sometimes you Google something, other times you ask someone. Merit to both in my view.
     
    Last edited: Oct 6, 2024
    • Like Like x 6
  6. Its not knocking the OP. Its suggesting that they could have found a solution quite easily.
    Yes theres merit to both, but I generally like to at least attempt to find an answer first.
     
    • Like Like x 3
  7. bakedpotatos.jm

    bakedpotatos.jm Well-Known Member

    Joined:
    Nov 10, 2020
    Messages:
    1,485
    Likes Received:
    2,653
    I did google but as someone who isn't tech savy it didn't make sense to me.

    I figured someone can explain it better here. So please.don't insunuate I'm stupid.

    Thanks.
     
    • Like Like x 13
  8. aeronautic237

    aeronautic237 Well-Known Member

    Joined:
    Sep 1, 2022
    Messages:
    3,202
    Likes Received:
    3,058
    Oh. Right. I'm sorry, I misinterpreted it.
     
  9. Nobodies insinuating youre stupid either.
     
  10. Spikee1975

    Spikee1975 Guest

    It helps if you try to visualise things by using analogies.

    Imagine a house with some apartments. There's families moving in there ( = memory allocation), soon the house is full. Now some families move on, because of jobs or other things, leaving the flats but don't "log out". Eventually, the house is deserted but is still listed as occupied, so the flats aren't for rent anymore.

    My coding skills are fairly basic (mostly from Amiga days, MC68k Assembler, BASIC and C), but one of the important thing was to make sure in your code each AllocMem() was somewhere followed by FreeMem(). In modern complex programs, this cannot be made sure anymore, which even coder legend John Carmack admitted (initially being against the use of GarbageCollectors because he said it would support lazy coding - but you cannot do without anymore due to the complexity of modern programs.)
     
    Last edited by a moderator: Oct 6, 2024
    • Like Like x 1
    • Helpful Helpful x 1
  11. iriv#7314

    iriv#7314 Well-Known Member

    Joined:
    Aug 23, 2021
    Messages:
    290
    Likes Received:
    256
    Hey Matt, now i'm wondering, in what is TSW5 being programmed?
     
  12. Spikee1975

    Spikee1975 Guest

    Not Matt, but UE/TSW is based on C++ code, which is the enhanced and object orientated version of original (procedural) C, with the TSW data being constructed of interconnected "blueprints" having nodes for input and output.

    The main difference between procedural and object oriented coding is that in procedural coding (C), you're giving more or less clear instructions, you have to know exactly how the program should act, resulting in very fast code (it's used for coding Operating Systems still), whereas in C++ you're basically defining objects and classes and let them interact based on their properties, which makes it easy to make games, as the way the objects interact is emerging from their given props.
     
    Last edited by a moderator: Oct 6, 2024
    • Like Like x 1
    • Helpful Helpful x 1
  13. knuckleshed

    knuckleshed Well-Known Member

    Joined:
    Jul 14, 2024
    Messages:
    441
    Likes Received:
    446
    With spelling like that I should hope not.
     
    • Like Like x 4
  14. Inkar

    Inkar Well-Known Member

    Joined:
    Dec 13, 2016
    Messages:
    1,627
    Likes Received:
    2,697
    There is a lot of disinformation in this thread. A memory leak happens when a programmer forgets to give back (free) memory that he previously reserved when it is no longer needed. That is the memory leak.

    If you continuously reserve memory but do not give it back, at some point you end up running out of memory to reserve.

    Usually, a user only really notices a memory leak if it is happening so frequently that it ends up causing an out of memory error, which most of the time causes the program to crash.

    Sometimes you will notice the program runs slow before crashing. That is because when the OS is close to running out of physical memory for the program it tries to use your (much slower) hard drive as a substitute. This is what is called using virtual memory.

    Of course this is a simplification of what really happens, but I feel is the level that most people can easily understand.
     
    • Like Like x 2
  15. Inkar

    Inkar Well-Known Member

    Joined:
    Dec 13, 2016
    Messages:
    1,627
    Likes Received:
    2,697
    Not exactly that. When you compile a C++ program you get assembly code, and its not very different from compiled C code. Objects help humans to understand how the data is organized, but your processor actually does not use them.

    The problem is that if you want Visual C++ to keep track of how you use the memory ("manage" your memory) it needs to add additional code that does it at compile time. This is done by Visual C++ automatically if you use "managed" memory, but it uses more memory and makes your code run slower.

    When you make a game, you usually need to run the code as fast as possible, because you need fast framerate, and you want to have as much memory available as possible.

    So programmers do not use automatically managed memory for games. They take care of it themselves (and sometimes make mistakes, as we all do).
     
    • Like Like x 2
    • Helpful Helpful x 1
  16. Winzarten

    Winzarten Well-Known Member

    Joined:
    Jun 27, 2020
    Messages:
    459
    Likes Received:
    905
    Depends on the platform. On PC, this is only the case with 32bit apps, as they only have 2GB virtual adress space. On 64bit? As memory virtualization is a thing, you will get into heavy stutters as OS will start to utilize page file more, but out of memory error generally happens only if the user disables page file, or limits its size.

    Unity is written in C#...
     
  17. Winzarten

    Winzarten Well-Known Member

    Joined:
    Jun 27, 2020
    Messages:
    459
    Likes Received:
    905
    Correction (I would edit, but forum thinks I'm a spammer :D)... Unity core is C++, but it's scripting language (what developers use to do game logic) is C#
     
    • Like Like x 1
  18. BeenTrain

    BeenTrain Active Member

    Joined:
    Dec 10, 2016
    Messages:
    135
    Likes Received:
    107
    To put it more simple (Via Programming Wisdom):

    "Memory leaks are like water leaks: they happen when you least expect it, take hours to locate and make you wish you had spent more time protecting your valuables." – Fred Heath
     
    • Like Like x 4
  19. bakedpotatos.jm

    bakedpotatos.jm Well-Known Member

    Joined:
    Nov 10, 2020
    Messages:
    1,485
    Likes Received:
    2,653
    Thank you all.
     
    • Like Like x 2
  20. Pipe

    Pipe Well-Known Member

    Joined:
    Jan 11, 2022
    Messages:
    2,063
    Likes Received:
    4,276
    upload_2024-10-7_8-42-4.png
     
    • Like Like x 2
  21. Princess Entrapta

    Princess Entrapta Well-Known Member

    Joined:
    Jul 23, 2021
    Messages:
    2,655
    Likes Received:
    3,395
    And sometimes you get classics like the old "Page fault in unpaged area" bluescreen.


    Also, on the topic of virtual memory, I remember when SSDs first began taking off commercially in the 2010s and the advice was always to disable windows' automatic virtual memory management, for fear of damaging the SSD through constant write cycles from using it for that. I'd always handled it manually myself, and tended to run systems with a minimum of 32GB of physical memory, so it was never an issue that came up.
     
    Last edited: Oct 7, 2024
  22. Now whose getting personal..
    hilarious.gif
     
  23. Gilly

    Gilly Well-Known Member

    Joined:
    May 22, 2019
    Messages:
    882
    Likes Received:
    1,973
    • Like Like x 2
  24. Redbus

    Redbus Well-Known Member

    Joined:
    Dec 8, 2016
    Messages:
    1,435
    Likes Received:
    3,037
    Aargh my spelling compulsion was also triggered but I resisted posting.
    Never mind, one for the road: #savvy
    ;)
     
    • Like Like x 1
  25. DTG Matt

    DTG Matt Executive Producer Staff Member

    Joined:
    Nov 17, 2016
    Messages:
    3,295
    Likes Received:
    18,497
    Locked, i think the question was answered and it's descended into a direction that isn't constructive.

    Matt.
     
    • Like Like x 6
    • Helpful Helpful x 1
Thread Status:
Not open for further replies.

Share This Page