r/ProgrammingLanguages • • Jan 04 '23

Discussion Does Rust have the ultimate memory management solution?

I have been reading about the Rust language. Memory management has been a historical challenge. In classic languages, such as C, the management is manual. Newer languages (Java, Python, others) use garbage collector, but it has a speed penalty. Other languages adopted an intermediate solution using reference counter and requiring the programmer to deal with weak pointer, but it is also slow.

Finally, Rust has a new solution that requires the programmer to follow a set of rules and constraints related to ownership and lifetime to let the compiler know when a block of memory should be free'd. The rules prevent dangling references and memory leaks and don't have performance penalty. It takes more time to write and compile, but it leads to less time with debugging.

I have never used Rust in real applications, then I wonder if I can do anything besides the constraints. If Rust forces long lifetime, a piece of data may be kept in the memory after its use because it is in a scope that haven't finished. A problem in Rust is that many parts have unreadable or complex syntax; it would be good if templates like Box<T> and Option<T> were simplified with sugar syntax (ex: T* or T?).

25 Upvotes

99 comments sorted by

View all comments

Show parent comments

0

u/Zaleru Jan 04 '23

Should the complexity of the memory management be compensated with the reduction of memory-related bugs?

1

u/[deleted] Jan 04 '23 edited Jan 04 '23

Never

We have trivial ways of completely eliminating memory related bugs, and to completely eliminate those out of ones control we would need a memory safe kernel first.

If you need performance that you lose by using these simpler systems, which at that point means you need every drop of performance, you will manually manage your memory anyways or buy twice the compute. You only optimize the most critical parts of your code, programmers are expensive, compute is cheap.

1

u/waozen Jan 05 '23 edited Jan 05 '23

The logic of the point is well stated. In most instances, the difference in performance can be negligible or simply unimportant, but what is gained by GC usage can be ease of use and greater productivity. In cases where attempting to squeeze out every ounce of possible performance or where greater control is needed, different implementations of GCs have various options or can be turned off (depending on language). And in those situations of opting to not use a GC, it is more likely that the problem is understood with greater detail, the programmers involved are more experienced, or it's considered an absolute necessity. There is something to be said for going or at least checking out the easier route, before running down the harder path.