• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

What IS a leak?

Status
Not open for further replies.
Level 8
Joined
Jul 8, 2013
Messages
249
Hi everyone, I've got a question I've been wondering about for a while.

Although of course I know what the effects of leaks on games are and how to avoid them, I've never really understood what a leak actually IS besides the standard 'a lost piece of memory'.

Could someone who knows the computer science behind leaks explain what they really are and what causes them?
 
Pointers like varibles always point to specific parts of your memory. If you destroy or change the pointers without clearing the memory part where it had pointed at, it will still will be counted as 'used' memory.

But now you have no pointer, and so no chance anymore to refer to this part of memory, so you can call it a memory leak.
 
Level 26
Joined
Sep 26, 2009
Messages
2,427
Basically, imagine PC memory as an empty storage room and anything that takes up a bit of memory as a box that you put inside storage room.
Now some boxes are opened/used only once, then they will never ever be used again and have no further meaning... so why keep them right? They just fill your storage room for no reason.

These "boxes" which will never be used again, but for some reason you left them inside storage room, are these memory leaks. They just fill your storage room and take space.
You want to have as empty storage room as possible, so you can put inside bigger and more important boxes.
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
What everyone has written basically summarizes what a "leak" is (at least with respect to war3 mapmaking).

I would say that the cause of all leaks when making a map is human error. From my readings Blizzard did not implement automatic memory management, so it is up to the coder to know when an object is no longer needed. Leaks come about when the human writing the code failed to deallocate memory for some object, which now takes up space but is serving no useful purpose at all. Over time, especially in functions that get re-used (basically any procedure that your map re-uses always), the leaks build up and take up memory. As the available memory gets smaller, you'll probably notice performance decreases (possibly because the hard drive is being used to store the additional memory, which is thousands of times slower, though I don't know the internals of warcraft 3 memory management).

However, just because a map gets "laggy" does not necessarily mean there are any leaks. If you are doing some intensive computations or functions that require a lot of space, then during those calls you'll inevitably notice a slowdown. But after that computation is performed, things should return to an acceptable FPS.

Leaks on the other hand will never go away, and if you accumulate enough of them the map can become unplayable.

So basically whoever wrote the script forgot to destroy a handle they no longer need (usually a local variable whose type is not a real, integer, boolean, etc.). The tricky part is it's not immediate obvious that a leak exists (you could have a 30 minute game and no noticeable FPS drop, but then suppose a game drags out to 60 minutes, and bam you suddenly notice a FPS drop).

I think there some instances where leaks exist but cannot be addressed by the coder (these would be inherent faults with the JASS, which is ultimately the fault of some human at Blizzard), though I don't know of any objects in JASS or situations which a leak is guaranteed and can't have its memory freed (I could be wrong).
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Code:
//this is just a declaration so that main can see test
void test();

//this is the first thing that runs in the program
//it calls a function called test
int main() { test(); return 0; }

//test calls malloc, which allocates memory
//it tells malloc to allocate 35 bytes
void test() { malloc(35); }

//what happens?
//main calls test
//test allocates 35 bytes
//malloc returns the allocate memory, but what is test doing with it? nothing
//as a result, every time test is called, 35 bytes are created
//do they ever get destroyed? There is no magic in the background here.
//if you don't see it, it doesn't happen.
//a leak is memory that is created and no longer referenced by anything
//the 35 bytes are created and aren't referenced by anything -> leak

As wikipedia puts it
In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations.[1] In object-oriented programming, a memory leak may happen when an object is stored in memory but cannot be accessed by the running code.[2]

So in short, a memory leak is dynamic memory that can't be accessed by the running code.

dynamic vs static memory is another topic altogether.

Here is the wikipedia code example
Code:
#include <stdlib.h>
 
void function_which_allocates(void) {
    /* allocate an array of 45 floats */
    float * a = malloc(sizeof(float) * 45);
 
    /* additional code making use of 'a' */
 
    /* return to main, having forgotten to free the memory we malloc'd */
}
 
int main(void) {
    function_which_allocates();
 
    /* the pointer 'a' no longer exists, and therefore cannot be freed,
     but the memory is still allocated. a leak has occurred. */
}

Here is the Princeton definition
A memory leak, in computer science (or leakage, in this context), occurs when a computer program consumes memory but is unable to release it back to the operating system. A memory leak has symptoms similar to a number of other problems (see below) and generally can only be diagnosed by a programmer with access to the program source code; however, many people refer to any unwanted increase in memory usage as a memory leak, though this is not strictly accurate.

http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Memory_leak.html

Most every page talking about memory leaks either uses the information from wikipedia or from Princeton.

you wanted a more CS view on it : p
 
Status
Not open for further replies.
Top