Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

That's what all the C engineers told me about manual memory management.

Of course, there are cases where they were right.



sort by: page size:

Totally, that could absolutely be the case. But it probably won't be. The reality is that most C programs do use manual memory management, by a very wide margin.

This is by definition manual memory management.

Genuinely curious on what people’s thoughts are on ever using manual memory management?

Are there many large programs (outside of embedded) where it ever really makes sense?

I ask because I am a fan of C, and I am sure there will always be old legacy C code, but is manual memory management essentially antiquated?


I believe you, but I haven't encountered anything like this in my work in a higher-level language, and this is my first push into manual memory management since I studied C in college many years ago.

Can you offer some examples of times when it has been indispensable, so that I can understand when this is useful?


Manual memory management: It's all fun and games until OOM errors come knocking, or- Segmentation Fault: Core Dumped.

I rest my case. There's a place for manual memory management. It's not in application code.


I have over a decade of professional experience entirely in C and C++. I believe that manual memory management is the wrong choice for the vast majority of applications. It is error prone and often slower than garbage collection.

Doesn't that effectively amount to manual memory management? What particular languages are you referring to?

Manually managing memory is idiomatic for performance engineering in C++. You don't always need to do it but it is definitely done.

Manual memory management.

No, manual memory management is what you do in C++.

Manual memory management is for the birds. Use something like Boost's shared pointers and you never need to worry about it.

Thanks. I just made a note of that!

So, someone else dug into the details of how C manages memory and wrote some code to help people find problems; makes good sense.


It somewhat depends on what you mean by "manual memory management", I am not sure it has a definition beyond garbage collectors being outside it -- gray areas exist. Using that tool requires some mastery of its idioms and rules regardless. Memory management is much more complicated than safely acquiring and releasing memory for object instantiation, or even accessing it safely. At least in C++, you can build safe memory abstractions for almost any resource model, including some pretty unusual ones that show up in performance-sensitive applications. In C it is significantly more difficult.

If you are building large-scale, high-performance infrastructure, manual memory management is to some extent unavoidable. In many database engines, for example, memory lifetimes and object lifetimes are distinct and independent concepts. There are classes of algorithm optimization that require strict and explicit understanding of the address space your process claims to own. The silicon does not respect the object model of your programming language, but you still need the silicon to manipulate memory directly (as it understands it) for performance reasons. This seems complicated but understanding it well enough to leverage it has significant performance implications.

That said, I don't see a lot of heap allocation in things like database engines. Almost all the memory used is allocated and deployed at bootstrap, and most of the remainder is on the stack. Not a lot of attack surface for memory management problems to show up. Other applications may be different.


Neat. Doesn't really change anything, except that you aren't required to manually manage memory but, practically speaking, you will end up manually managing memory. Especially if you're choosing C for performance requirements.

I worked with manual memory management for a decade (24/7 systems) and don’t miss it. It’s not per-se hard, it’s not scary, but if you’re dealing with structures that may contain reference loops, or using an architecture based on event handlers that may be moving references around, you need to do some very careful design around memory management instead of just thinking of the problem domain.

That specific issue exists in manual memory management schemes as well.

Nope, you're the one missing the point, just because a language has some kind of automatic memory management, it doesn't mean C like features for deterministic manual memory management, or any other kind of manual resource management aren't available.

Learn to use them, and then you'll get the point.


Memory management in C.

Manual memory allocation in the C programming language.
next

Legal | privacy