tags:

views:

1132

answers:

16

With RAM typically in the Gigabytes on all PC's now, should I be spending time hunting down all the small (non-growing) memory leaks that may be in my program? I'm talking about those holes that may be less than 64 bytes, or even a bunch that are just 4 bytes.

Some of these are very difficult to identify because they are not in my own code, but may be in third party code or in the development tool's code, and I may not even have direct access to the source. In those cases, it would involve lengthy communication with the vendors of these products.

I have seen the number one memory leak question here at SO: Are memory leaks ever ok? and the number one answer to that, as of now voted up 85 times, is: No.

But here I'm talking about small leaks that may take an inordinate amount of debugging, research and communication to track down.

And I'm only talking about a simple desktop app. I understand that apps running on servers must be as tight as possible.

So the question I am really asking is, if I know I have a program that leaks, say 40 bytes every time it is run, does that matter?

A Single Drip


Also see my followup question: What Operating Systems Will Free The Memory Leaks?


Postscript: I just purchased EurekaLog for my program development.

I found an excellent article by Alexander, the author of EurekaLog (who should know these things), about catching memory leaks. In that article, Alexander states the answer to my question very well and succinctly:

While any error in your application is always bad, there are types of errors, which can be not visible in certain environments. For example, memory or resources leaks errors are relatively harmless on client machines and can be deadly on servers.

+1  A: 

Yes, it matters. Every little leak adds up.

For one, if your leaky code is used in a context where it is repeatedly used, and it leaks a little bit each time, those little bits add up. Even if the leak is small, and infrequent, those things can add up to significant quantities over long periods of time.

Secondarily... if you're writing code that has memory leaks, then that code has problems. I'm not saying that good code doesn't from time to time have memory leaks, but the fact of their existence means that there are some serious problems going on. Many, many security holes are due to just this sort of oversight (unbounded string copy, anyone?).

Bottom line is, if you know about it, and don't do all you can to track it down and fix it, then you're causing problems.

McWafflestix
40 bytes from my program x 10 times a day = 400 bytes. Wow! Will anyone notice?
lkessler
I know! By the way, your bank called, and they said they've been losing .02 percent of your account balance every day. They figured you wouldn't notice.
McWafflestix
40 bytes from my program x 10 times a day wont be 400 bytes. Correct me if i am wrong, but dont all modern operating systems reclaim memory when programs terminate ?
Andrew Keith
Excellent comment, Andrew. I don't know the answer to that. Maybe someone can elaborate.
lkessler
"do all you can to track it down and fix it" -- no. Do *as much as is appropriate* to track it down and fix it. Suppose a paint program leaks 40 bytes every time I save a file. Do I want the maintainers to focus on fixing that leak or on adding new features? How many files do I save in one session? Five? Ten? A thousand? It's insignificant either way. A programmer who "did all he could" to fix that leak, at the expense of doing something useful, would be wasting his time and harming the product. Of course the same leak in a 24x7 server *would* be a priority to fix: but context!
itowlson
@McWafflestix, The problem with your bank metaphor is that at the end of the day, the operating system reclaims the memory lost. It's like the bank calls you and says, "Hey, your bank account with $10 000 on it, we lost $0.02 a few hours ago, but they are back now".
Marius
@marius: Yes, I understand that the memory is reclaimed; but to continue (over-)using the bank metaphor, it's as though you didn't get interest on that money while the bank didn't have it. Or, more appropriately, you didn't have that money available. Sure, the few cents might not matter; but MAYBE they make the difference between an overdraft fee or not. Sure, maybe the few leaked bytes usually don't matter, but what if they're holding a memory page that forces a pagefault that slows down a critical process?
McWafflestix
I should clarify here; I don't think it's acceptable to just "let memory leaks go". Do they happen? Yes. Is it always the best idea to fix them? No. But should you care about whether or not your code leaks memory? Hell yes. It's just like bugs. Bugs happen, but you shouldn't take a cavalier attitude towards your code and letting bugs creep in.
McWafflestix
+5  A: 

Yes. Leaks matter. If your apps runs 24x7x365 and handles a few thousands transactions per second, a few bytes turns into gigabytes rapidly.

S.Lott
And if my app is just a desktop program, run manually by a user when they want to use it, then does it matter?
lkessler
@lkessler: If you're asking about best practices, memory leaks do not fall into that category. At least in your own code, be diligent in freeing allocated memory or disposing of unmanaged resources.
TrueWill
Yes still matters. What if they leave it open for an extended period of time. Over a weekend? While they go on vacation?
stimms
@stimms: Well, actually, that desktop app would most likely only affect that desktop, and the user would be away on holiday so, other than a reboot on return, no real downside. The fact that you shouldn't be leaving your desktop turned on when you're on holidays is another matter.
paxdiablo
No one runs a single desktop application. Watch real users. They run a dozen apps and leave them running for weeks. Seriously. I have to yell at my developers and insist they reboot every day. Most people do not stop applications or reboot unless there's a power failure. Core leaks matter.
S.Lott
I agree. My boss just told me a story where they sold a software which was used inside a production-line. It would crash every two or three weeks because it slowly filled up the RAM. As you might imagine the customer was not to happy to have his production stalled, which was supposed to run 24/7 and would produce half an hour of garbage every time it was started up.
Lucas
A few years back someone asked for help because their web server only ran for 20 minutes before it crashed. The first thing I said was "stop using C++". They were amazed that I could have known that they were using C++. C++ is a core leak waiting to happen.
S.Lott
+2  A: 

It depends on the nature of your application. I work primarily with web sites and web applications. So by most definitions, my application "runs" once per request. Code that leaks a few bytes per request on a high volume site can be catastrophic. From experience, we had some code which leaked a few kb per request. Added up, that caused our web server worker processes to restart so often it caused minute-long outages throughout the day.

But web applications (and many other kinds) have an indefinite lifespan - they run continuously, forever. The shorter-lived your application, the better. If each session of your application has a finite and reasonably predictable end point, there's of course a reasonable amount of leakage you can tolerate. It's all about the ROI.

Rex M
+4  A: 

I am in the same boat as you. I have small memory leaks that don't grow ever. Most of the leaks are caused by improperly tearing down COM objects. I have studied the leaks and come to realize the time and money to fix them is disproportional to the damage the leaks do. Windows cleans up most of the time so the true damage is only realized if the user runs his computer for years without rebooting.

I think it's acceptable to leave in the leaks. It sounds so taboo, but if the leaks never ever ever grow and they are small, it's pretty insignificant in the larger scheme of things.

Andrew Keith
Well, it's not really a leak if it doesn't grow. If your leak is "improperly tearing down COM objects" and it happens N times, that's one leak that grows, not N leaks that don't grow.
paxdiablo
+29  A: 

Leaks are bugs.

You probably have other bugs too.

When you ship a product, you ship it with known bugs. When you choose which (of the known) bugs to "fix" versus "ship with", you do so based on the cost and risk to fix versus the customer benefit.

Leaks are no different. If it's a small leak that happens during an infrequent operation in a non-server app (e.g. an app that runs for minutes or hours and then shuts down), it might be "ok" in the same way any other bug is ok.

Actually, leaks can be kinda different in one important way, which is that if you are shipping a library/API, you really should fix them, because the customer benefit is enormous (otherwise all your customer 'inherit' your leak, and will be phoning you just as you have to do to talk to 3rd party vendor now).

Brian
I'm of the opinion that the determination as to where the cost / benefit comes on this issue (fix vs. leave) generally comes down to management; I generally try to not try to tell management that I've been generating code that leaks memory (by not writing it).
McWafflestix
+17  A: 

While I agree that every little leak adds up, I don't agree that it's always the best business decision to fix it.

What if you have a stateless legacy system and no coders who understand it? Now you are using it in a situation that has to scale... and it's 100X cheaper to spawn a new instance and swap them out before memory goes overboard.

Or let's say you have a batch processing system that runs 24x7 but for which there is no real user. If it's cheaper to monitor memory and tell the system to restart itself periodically, why hunt down the leak?

I think you should try real hard but be pragmatic about the business ramifications of the decision.

Benjamin Cox
Yay for a bit of pragmatism.
itowlson
+1. Most (every, I believe, but I've got an open mind) decision has a cost and benefit. All decisions should weigh up those two. A particular example from the past was a leak in a server application that we figured would take a couple of weeks to track down and fix. We instead implemented an "automatically restart the server during quiet time" solution which worked fine. It was deemed better to inform the users that they couldn't use the system between 12:10am and 12:20am than to spend the money fixing it. Especially since it was mostly a 9-to-5 business.
paxdiablo
+9  A: 

No, it does not matter, however, only if, as you pointed out, the memory leak must not be repetitive. Memory leaks that don't grow as a program progress is usually okay. Non-growing memory leaks will eventually be solved when a process terminate.

However, it is difficult to prove an observed memory leak is not growing; you have sufficient empirical data. In reality, many huge program (even written in Java/C#) have memory leaks, but most of them are non-growing leaks.

Seriously, we can't live without memory leaks, deadlocks, data races. Having these bugs itself are okay. Only when it kills your program, it matters.

But, I have to disagree with your opinion: "memory is cheap". That can't justify memory leaks. That's very dangerous.

minjang
+3  A: 

I agree with the earlier responses that leaks do matter.

People may have tons of memory, but they are also running more and more programs, and unless your application is completely hogging up the processor, it needs to play nice with other programs, which also means not hogging up resources it doesn't need.

So, this small memory leak will add up and mean that the user will have other problems, and if they decide they are having memory issues, if they decide that running your app causes them problems then they will stop running it.

Besides, as has been pointed out, if you don't know what is causing the leak then you may have other problems you don't know about. It may be the tip of a bug iceberg.

James Black
Exactly. I completely concur.
McWafflestix
+30  A: 

This is completely a personal decision.

However, if:

So the question I am really asking is, if I know I have a program that leaks, say 40 bytes every time it is run, does that matter?

In this case, I'd say no. The memory will be reclaimed when the program terminates, so if it's only leaking 40 bytes one time during the operation of an executable, that's practically meaningless.

If, however, it's leaking 40 bytes repeatedly, each time you do some operation, that might be more meaningful. The longer running the application, the more significant that becomes.

I would say, though, that fixing memory leaks often is worthwhile, even if the leak is a "meaningless" leak. Memory leaks are typically indicators of some underlying problem, so understanding and correcting the leak will often make your program more reliable over time.

Reed Copsey
+1 for actually understanding the question (and answering it).
Peter Mortensen
+1 for the last bit.
musicfreak
+5  A: 

A memory leak really depends on several things:

  • How often the leak happens
  • How much memory is lost each time
  • How long is the program going to run

For example, if you lose 40 bytes every time a task happens, and that task happens when the program starts, then nobody cares. If you lose 40Mb every time the program starts, then it should be investigated. If you lose 40 bytes every frame in your video or game engine, then you should look into that, because you'll lose 1.2kB each second, and after an hour you would have lost almost 4Mb.

It also depends on how long the program is going to stick around for. For example, I have a small calculator app, that I open, run a calculation in, and then close again. If that app loses 4Mb in it's run, then it doesn't really matter, because the OS will reclaim that lost memory once I close it. If the hypothetical video/game engine mentioned earlier lost 4Mb an hour, and it ran a demo unit, for several hours a day at a stand at a convention, then I'd look into it.

An example of a famous memory leak is Firefox, which lost a lot of memory in it's earlier versions. If your browser leaked memory 10 years ago, then you probably wouldn't care. You shut down the computer every day, and you while running the browser you only had one page up at a time. Today I just let my laptop go to standby, and I never close Firefox. It is open for weeks at a time, and I have at least 10 tabs open at any given time. If memory leaks every time a tab is closed, then that is going to build up to a larger leak now than it did 10 years ago, and so it is more important.

Marius
+3  A: 

Are memory leaks ever ok?

Sure, if it's a short-lived process.

Memory leaks over a long period of time are, as the 85-point answer implies, problematic. Take a simple desktop app, for example -- prior to versions 3.x, did you ever notice how you needed it reboot Firefox after a while to recover it from sluggishness?

As for the short term, no, it doesn't matter. Take CGI or PHP scripts for example, or the little Perl three-liner in your ~/bin directory. Nobody's going to call the memory police if you write a 30-line non-looping application in C with 5 lines of malloc() and not a single call to free().

a paid nerd
+2  A: 

It all depends. Reasons not to worry: the process is short-lived, the leaks are small and/or infrequent, the cost of an out of memory exception is low (eg, a web server instance in a cluster needs restarting and a few fetches need retrying). So I agree that some leaks don't really matter in practical terms.

But on the other hand, if you do have cause to worry, or even feel a nagging sense of doubt that maybe you're not taking quality seriously enough, it's a small matter (in most cases) to run your software with a memory leak detector and fix the problems. There are many good leak detectors out there. And you might find that the leak is part of a more serious problem, such as not releasing other resources (like open files). You may even find that the harmless leak would turn quite dangerous in usage scenarios you haven't tested yet.

Jim Ferrans
A: 

Memory leaks are never OK in any program, however small it may be. Slowly they will add up to fill up your entire memory. Suppose you have a calling system which leaks about 4 bytes of memory per call it handles. You can handle say, 100 calls a second (this is a very small number), so you end up leaking 400 bytes a second or 400x60x60(1440000B) an hour. So, even a small leak is not acceptable. And if you dont know the source of the leak then it may be some real big issue and you end up having buggy software. But, essentially it boils down to the questions like, for how much time the program runs and what number of times the leak happens. So, it may be ok it leaks a very small amount and is not repeated but still the leak may be a small part of a bigger problem. So, I feel that memory leaks are never ok.

mkamthan
A: 

That's like asking if there was a crack in a dam is it ok? NO! NEVER! Avoid memory leaks as if your life depends on it because when your application grows and gets more use that leak is going to turn into a flood and sooner or later someone or something is going to drown in it. Just because you can have lots of memory doesn't mean that you can take shortcuts with your code. Once you find a leak do what you can to resolve it and if you can't solve it make sure you keep coming back to it until it's fixed.

If you can't resolve the leak then try to see if you can clean up after it. The bigger issues come when the leak is repetitive.

Last note: if you ever hand the software to someone else and that leak is still there it may be a long time before someone else finds and/or fixes it.

Middletone
A: 

I wouldn't be so worried about the quantity but the frequency of memory which you leak, but if you leak even just a few bytes very very often, your malloc's data structures will grow and might make it dramatically slower to traverse them, to allocate new memory and free. Unless you hit the border where you have leaked more than a tiny fraction of your RAM, mainly your program will suffer under those performance problems and not the whole system. Does not apply to even remotely dlmalloc-based systems (FreeBSD, Linux, etc), there it's just don't care, all you loose there is memory (perhaps a few times more than the amount you think) and not performance.

A single allocation which is not reclaimed by your program is not a leak at all. If you write a small command line utility which takes a second to complete, you may not need to even reclaim any memory there. Upon termination, the OS reclaims RAM, file handles, should basically apply to any kind of system resource, but you cannot rely on some OSes as much as on others, but as long as it's just memory, even Windows 95 will manage it just right.

Oh and another thing follows from that, if you leak memory, don't bother cleaning up at the end of the program or after a long execution time, or you will just waste even more CPU time. Always fix the leaks as near to the timepoint where they are created as possible. Other reason: malloc implementations prefer to keep the RAM they got from the OS for future allocations instead of giving it back. Also you may suffer address space fragmentation.

3yE
A: 

If someone says memory leaks are ok in small amounts and as long as it doesn't crash the application, it is like saying, stealing is ok if in small amounts and as long as you are not caught :)

Sharjith N.