This question already has answers here:
What REALLY happens when you don't free after malloc before program termination?
(20 answers)
When you exit a C application, is the malloc-ed memory automatically freed?
(9 answers)
Closed 9 years ago.
int main()
{
char *a = malloc(1024);
return 0;
}
Does the program above have a memory leak? Please provide as complete and technical answer as it is possible.
The program itself has a leak. Whether the operating system cleans this up or not is a different matter. I suppose it's better to ask 'could this program potentially cause a problem on any system?' and the answer is absolutely yes.
The C standard nowhere says that memory allocated with malloc will be freed at program termination, either normal or abnormal. Compare that to open files, which are guaranteed to be closed for you by the C implementation if the program terminates normally.
Simple answer is yes. The OS is doing the tidying up for you
EDIT
For the benefit of gg.kaspersky.
When the program gets to the end you should have tidied up the heap. It says that you have considered all the allocations and provided the release of those resources (this can be said to be true for open files or other OS resources).
Is this complicated enough for you?
BTW - It is good manners when visiting a home (OS) you leave it in the same state as you arrived.
Your question is incomplete... and as such the best answer you can get is "it depends".
From a program scope, yes, you have a memory leak. That much should be obvious. From a system scope the answer is it depends on which operating system you're running on.
In general if you're programming on a main stream device (Win7/MacOSX/Linux desktop/laptop computer, iOS/Android device, etc) you're pretty safe. I can't think of any main stream modern operating systems which would not be able to clean up after you.
However, if you're working on some older or embedded systems, you will leak memory here. I've seen memory leaks on a uCLinux platform (an OS specifically designed for hardware which does not have an MMU[Memory Management Unit]).
Dynamic memory is generally considered dangerous, which is why it's not legal to use it on some RTOSs. If you know the scope of deployment of your application throughout the lifetime of its use, you're fine to code like this... but the odds are you don't, and you shouldn't make assumptions about what the OS will do. Always clean up your own mess.
I wouldn't consider that a memory leak. The memory is still accessible through a variable that is still in scope when you exit.
Freeing all memory you allocated at the end of a program is just being unnecessarily pedantic and makes you spend time doing something that the operating system will do for you much more efficiently.
Edit (technical details):
With some implementations of malloc doing unnecessary freeing of memory at the end of a program can be horribly inefficient. All malloc implementations that keep their accounting information inline (through boundary tags or a similar mechanism) will touch most, if not all, pages that you have allocated. Normally this isn't a problem besides dirtying cache lines and TLB entries that are being thrown away, but couple this with a program that used enough memory to start using swap and you end up swapping in a lot of memory just to free it. The operating system can do this much more efficiently when you exit (by just marking the swap slots as free).
If you have a malloc implementation that returns memory to the operating system, freeing the memory will cause the operating system to unmap the memory which in turn requires expensive TLB operations if you're on a multiprocessor system. On exit the invalidation can be done much more efficiently (either by doing a full TLB flush or invalidating the TLB tag).
Related
This question already has answers here:
What REALLY happens when you don't free after malloc before program termination?
(20 answers)
Closed 6 years ago.
Suppose that I set a pointer in order to have a string, eg:
char *string = NULL;
size_t size = 0;
getline(&string, &size, stdin);
Is necessary to do a free(string) before end the program?
As I could see in man, getline calls to malloc() and I have supposed it.
To ensure your software doesn't have any memory leaks, yes, you should call free for any memory that you have allocated dynamically.
Memory will be reclaimed by the OS when the process terminates, and so it doesn't matter much unless you are running low on available memory. However, using free to release memory is very important if you're writing software that will run in the background, such as a daemon process.
It is recommended (almost universally) that the Right Way (tm) is to free your memory and not to rely on the OS to reclaim that space.
It is not necessary.
There are some good reasons to, as described in another answer. But there are also some reasons not to: the code to do the freeing takes extra time to write, debug, and maintain, and is an extra potential source of bugs.
It's not possible to give a universal answer that you should or shouldn't: it's a choice that each project has to make, depending on its own circumstances.
What happen if I end the execution by passing return 0; after using a malloc and without freeing the part of memory allocated?
int * var;
var = (int *)malloc(sizeof(int)) ;
free(var) ;
return 0;
The program contains a memory leak, as explained here. To answer the question specifically, the effect of the memory leak depends on the environment; in the best case, nothing happens, in the worst case, the machine might crash sooner or later. The existence of the memory leak is to be regarded as a bug in any case.
It is implementation specific.
On most operating systems (notably desktop or server OSes, e.g. Linux, MacOSX, Windows...) once a process is terminated, every used resources is released, and that includes its virtual address space. Hence even unfreed heap memory is released.
In particular, if you code a quickly running program (e.g. you know that it always will run in less than few seconds), it might be easier to accept some memory leak (but if you do, please comment that in your code and/or documentation). And many real life programs are doing that (in particular the GCC compiler, and probably several Unix shells).
On the contrary, if you are coding a server (e.g. a database or compute server), you should avoid any memory leaks which would make the server's process RSS grow indefinitely (till some crash). You usually should take great care that every allocated heap memory to deal with one request gets free-d after replying to that request.
On some embedded operating systems, if you don't release all the resources explicitly (free all heap allocated memory, fclose all opened streams) and properly, you have a resource leak.
See also this related question. On many OSes (including Linux) you can use valgrind to hunt memory leak bugs. With a recent gcc you might use debugging options like -g -fsanitize=address
Read also (at least for the concepts and the terminology) something about garbage collection and about fragmentation. If programming in C, you might consider using Boehm's garbage collector.
There is however a very good practical reason to systematically free all previously malloc-ed memory: it is a good programming discipline, and it helps a lot using tools like valgrind which helps debugging genuine memory bugs. Also it makes your code more clean, and you could reuse it in some different contexts (e.g. as part of some library, usable in long-running processes).
If you don't free the memory, your application will have a memory leak. So if you don't free the memory and leave the application on for a couple of days, it will start to slow down, and eventually crash.
This question already has answers here:
When you exit a C application, is the malloc-ed memory automatically freed?
(9 answers)
Closed 8 years ago.
I am using Visual C++ 2010 for a C project. I have the following code in main():
ARRAY2D a;
arr2_init(&a, 5, 5); /* 5x5 dynamic, multi-demensional array (of type int) */
arr2_release(&a);
I'm not sure if I need the last line. Can I omit out arr2_release() at the end of the program in modern OS's? I'm using Windows 7.
Yes, you can avoid releasing any resource manually which the runtime or the OS will clean up after you.
Still, do not do so please.
It is a valid optimisation for faster shutdown (and sometimes even for faster execution in exchange for memory consumption), though you must be picky about which resources you leave around:
Memory and file descriptors are efficiently handled by the OS (ancient platforms not doing so have mostly succumbed to disuse. Still, there are a few tiny systems not able to free this).
FILE buffers are efficiently cleaned up by the runtime.
Windows GUI resources are not efficiently cleaned up this way, it needs longer.
Anyway, do the cleanup and develop the right mind-set, it makes searching for leaks much easier and is better transferable to bigger and longer-running tasks.
Premature optimisation is the root of all evil. (The expert only option to optimise after measurement and careful consideration does not apply yet)
Always free your memory. The operating system will release a process' resources when it terminates, which includes its memory. But that doesn't give you garbage collection (you have to use different languages for that). Also note that it only does so after your program has ended (also stated in the comments), so as long as your program is running, the memory will not be freed if you don't do it.
Your code might be used as part of a bigger program someday, even if it's now only a few lines. So always make sure to release all resources you acquire. Also, as a C programmer, thinking about resource management should be a habit anyway.
The OS will just recover it (after the program exits) right? So what's the use other than good programming style? Or is there something I'm misunderstanding? What makes it different from "automatic" allocation since both can be changed during run time, and both end after program execution?
When your application is working with vast amounts of data, you must free in order to conserve heap space. If you don't, several bad things can happen:
the OS will stop allocating memory for you (crashing)
the OS will start swapping your data to disk (thrashing)
other applications will have less space to put their data
The fact that the OS collects all the space you allocate when the application exits does not mean you should rely upon this to write a solid application. This would be like trying to rely on the compiler to optimize poor programming. Memory management is crucial for good performance, scalability, and reliability.
As others have mentioned, malloc allocates space in the heap, while auto variables are created on the stack. There are uses for both, but they are indeed very different. Heap space must be allocated and managed by the OS and can store data dynamically and of different sizes.
If you call a macro for thousand times without using free() then compiler or safe to say system will assign you thousand different address, but if you use free() after each malloc then only one memory address will be given to you every time.
So chances of memory leak, bus error, memory out of bound and crash would be minimum.
Its safe to use free().
In C/C++ "auto" variables are allocated on the stack. They are destroyed right at the exit from the function. This will happen automatically. You do not need to write anything for this.
Heap allocations (result of a call to malloc) are either released explicitly (with a call to free) or they are cleaned up when the process ends.
If you are writing small program that will be used maybe once or twice, then it is ok not to free your heap allocations. This is not nice but acceptable.
If you are writing medium or big project or are planning to include your code into other project, you should definitely release every heap allocation. Not doing this will create HUGE trouble. The heap memory is not endless. Program may use it all. Even if you will allocate small amount of memory, this will still create unnedded pressure on the OS, cause swapping, etc.
The bottom line: freeing allocations is much more than just a style or a good habit.
An automatic variable is destroyed (and its memory is re-usable) as soon as you exit the scope in which it is defined. For most variables that's much earlier than program exit.
If you malloc and don't free, then the memory isn't re-usable until the program exits. Not even then, on some systems with very minimal OS.
So yes, there's big difference between an automatic variable and a leaked memory allocation. Call a function that leaks an allocation enough times, and you'll run out of memory. Call a function with an automatic variable in it as many times as you like, the memory is re-usable.
It is good programming style and it's more than that. Not doing proper memory management in non-trivial programs will eventually influence the usability of your program. Sure the OS can reclaim any resources that you've allocated/used after your program terminates, but that doesn't alleviate the burden or potential issues during program execution.
Consider the web browser that you've used to post this question: if the browser is written in a language that requires memory management, and the code didn't do it properly, how long do you think it would be before you'd notice that it's eating up all your memory? How long do you think the browser would remain usable? Now consider that users often leave browsers open for long periods of time: without proper memory management, they would become unusable after few page loads.
If your program does not exit immediately and you're not freeing your memory you're going to end up wasting it. Either you'll run out of memory eventually, or you'll start swapping to disk (which is slow, and also not unlimited).
automatic variable is on the stack and its size should be known on compilation time. if you need to store data that you don't the size, for example, maintain a binary tree, where the user add and removes objects. beside that stack size might be limited (depends on your target), for example, linux kernel the stack is 4k-8k usually. you also trash the instruction cache, which affects performance,
Yes you absolutely have to use free() after malloc() (as well as closing files and other resources when you're done). While it's true that the OS will recover it after execution, a long running process will leak memory that way. If your program is as simple as a main method that runs a single method then exists, it's probably not a big deal, albeit incredibly sloppy. You should get in the habit of managing memory properly in C because one day you may want to write a nontrivial program that runs for more than a second, and if you don't learn how to do it in advance, you'll have a huge headache dealing with memory leaks.
what is the most efficient and accurate way/ API to measure heap memory consumption from the same running process programmatically? I want to estimate (as accurately as is reasonably possible) how much memory has been new or malloc since startup, minus the memory that has been free or delete
the scope of the question is linux and possibly other linux environments. The language is either C or C++
EDIT
It is enough for my purposes to know the actual number (and size) of allocated/held blocks by any malloc implementation, i don't need the detail of actual malloc memory minus the the freed memory
Assuming new uses malloc look here.
For more details of a processes memory allocation, look at the /proc/[pid]/maps.
Also note that linux implements copy-on-write. This means that sometimes processes can share memory. This is especially true if the process was forked without calling exec afterwards.
You can use mallinfo for an estimation. I've just found this, not sure whether this is process or system.. :/
I'm not totally sure what you are asking, malloc minus freed is less than the actual usage because of the memory fragmentation, if you really need that number you have to use custom allocators (which are tiny wrappers around existing ones) everywhere in your code which is going to be painful.
Have you considered reading from /proc/u/stat? (where "u" is your pid)
If you use valgrind and run your program to completion, it gives you a report on memory usage.
http://valgrind.org/
You can get quite a bit of information about your heap usage by linking against tcmalloc from Google Perftools. It is designed to locate memory leaks and determine "who the heck allocated all that RAM", but it provides enough instrumentation to answer most questions you might have about your heap.