News for hackers. Stuff that matters.
Re:What every programmer should know about memory (2007) by jheriko on Friday November 20, 2015 @11:21AM (#10601990)
there are just three things i wish programmers knew about memory... none of them are especially low-level or require depth of understanding.

never dynamically allocate it unless forced.

really. never allocate at run-time it unless you are absolutely forced by your algorithm.

if you absolutely really must allocate memory on the fly, have a budget so you can do one big up-front allocation and carefully reuse it. even better, don't allocate it to start with.

Re:What every programmer should know about memory (2007) by progmal1 on Friday November 20, 2015 @11:32AM (#10602053)
I feel like you are projecting your domain specific experience on a larger more general audience than is appropriate.
Re:What every programmer should know about memory (2007) by jheriko on Monday November 30, 2015 @02:51PM (#10651321)
you are probably right
Re:What every programmer should know about memory (2007) by yongjik on Friday November 20, 2015 @12:04PM (#10602280)
That will basically prohibit using most of C++ STL, for example. Premature optimization is the root of...
Re:What every programmer should know about memory (2007) by jheriko on Monday November 30, 2015 @02:52PM (#10651326)
i'm not a fan of that religious statement. its assuming it is premature.

STL is not too bad either. you can make it work with budgets too... implementing a custom allocator is a pain, but there are lots of ways to not write code that imagines that memory is an infinite resource...

Re:What every programmer should know about memory (2007) by rootlocus on Friday November 20, 2015 @11:34AM (#10602068)
I'll remember that the next time I'm using python.
Re:What every programmer should know about memory (2007) by azinman2 on Friday November 20, 2015 @12:06PM (#10602290)
Or Java or go or ruby or Scala or php or....
Re:What every programmer should know about memory (2007) by vvanders on Friday November 20, 2015 @12:18PM (#10602365)
Still very much relevant in Java if you don't want your GC to churn like mad.

Heck even some of the cache aware algorithms can be taken advantage of with some smart use of ByteBuffers to get a 10-50x improvement in performance.

Re:What every programmer should know about memory (2007) by azinman2 on Friday November 20, 2015 @01:35PM (#10602923)
Few applications need this... mostly in the financial space. The GC is quite good at quick temp objects these days.
Re:What every programmer should know about memory (2007) by jheriko on Monday November 30, 2015 @02:58PM (#10651367)
i'm not sure using a dynamic late scripting language is an excuse for pretending memory is infinite. :)

but yes... i do have frustration from "highly skilled native programmers" who seem to think that memory is a boundless resource and then complain that something like 32GB is a constrained memory environment when it is the exact opposite.

Re:What every programmer should know about memory (2007) by aidenn0 on Friday November 20, 2015 @12:10PM (#10602317)
You do know that function stack frames are dynamically allocated (just not from the heap in most C and C++ implementations).
Re:What every programmer should know about memory (2007) by jheriko on Monday November 30, 2015 @02:56PM (#10651346)
in a sense yes, but its not a realistic way to use the term.

what happens is that the stack is once dynamically allocated up-front when the thread is created and an index into that blob of memory is incremented and decremented as appropriate (stack pointer).

you can call that allocation, and it would be accurate but its not what i meant at all. it also avoids growing your memory consumption and doesn't impact memory budgets no matter how much stack you try and use (you will overflow it instead... and will need to allocate a bigger budget upfront for the stack with a compiler flag)

Re:What every programmer should know about memory (2007) by Namrog84 on Friday November 20, 2015 @01:39PM (#10602947)
Maybe he is coming from the fact that lots of things still leak memory this day and age. And you tend to not leak memory when you avoid dynamic allocation?

Though you can still build up crud depending upon the system.

Re:What every programmer should know about memory (2007) by rikkus on Friday November 20, 2015 @01:28PM (#10602882)
It would help to give reasons why rather than simply commandments.

I'll give one: Performance takes a hit with dynamic allocation. Deallocation also has to occur and if you use GC you may experience pauses as this happens. Your GC may vary. The performance inpact of dynamic allocation may of course be completely acceptable when traded against productivity gains.

Re:What every programmer should know about memory (2007) by jheriko on Monday November 30, 2015 @03:01PM (#10651390)
true. i wasn't just thinking about performance but also budgetting.

using memory as an endless resource without thinking seems to be standard practice rather than something that is considered "sloppy".

there is also the practice involved. run-time allocation is a bit of a golden sledgehammer and a double edged sword... sure you can write code more quickly, but you are also being enabled to write worse code (i.e. leaks). i don't think we should take the power away, but respect it more...