146 votes
Accepted

When to optimize for memory vs performance speed for a method?

Instead of speculating about what may or may not happen, let's just look, shall we? I'll have to use C++ since I don't have a C# compiler handy (though see the C# example from VisualMelon), but I'm ...
Phil Frost's user avatar
  • 1,569
109 votes
Accepted

If I need to use a piece of memory throughout the lifespan of my program, is it really necessary to free it right before program termination?

If I need to use a piece of memory throughout the lifespan of my program, is it really necessary to free it right before program termination? It is not mandatory, but it can have benefits (as well as ...
Doc Brown's user avatar
  • 201k
66 votes

When to optimize for memory vs performance speed for a method?

To answer the stated question: When to optimize for memory vs performance speed for a method? There are two things you have to establish: What is limiting your application? Where can I reclaim the ...
Berin Loritsch's user avatar
50 votes
Accepted

How much stack usage is too much?

It depends on your operating system. On Windows, the typical maximum size for a stack is 1MB, whereas it is 8MB on a typical modern Linux, although those values are adjustable in various ways. If the ...
Sebastian Redl's user avatar
45 votes

When to optimize for memory vs performance speed for a method?

"this would reduce memory" - em, no. Even if this would be true (which, for any decent compiler is not), the difference would most probably be negligible for any real world situation. However, I ...
Doc Brown's user avatar
  • 201k
33 votes

When to optimize for memory vs performance speed for a method?

You can do better than both of those with return (abs(a + b) > 1000); Most processors (and hence compilers) can do abs() in a single operation. You not only have fewer sums, but also fewer ...
Graham's user avatar
  • 1,996
31 votes
Accepted

Redux memory consumption

This is a valid concern. While I have not measured the memory usage of Redux applications, I think that before committing to use Redux (or any other framework for that matter) you should create stress ...
Dan's user avatar
  • 2,902
22 votes

How much stack usage is too much?

The only valid answer is vague: "too much is when the stack overflows." Unless you are in complete control over the implementation of every line of code between the program's entry point and the ...
Blrfl's user avatar
  • 20.3k
17 votes

How important is memory alignment? Does it still matter?

Yes, memory alignment still matters. Some processors actually can't perform reads on non-aligned addresses. If you're running on such hardware, and you store your integers non-aligned, you're likely ...
Matthew Walton's user avatar
16 votes

When to optimize for memory vs performance speed for a method?

When is it appropriate to use Method A vs. Method B, and vice versa? Hardware is cheap; programmers are expensive. So the cost of the time you two wasted on this question is probably far worse than ...
John Wu's user avatar
  • 26.1k
15 votes

Why do we need to specify the type of data a pointer will hold, if all pointers are the same

From the memory-allocation point-of-view, you're right. A pointer variable on a 64-bit architecture occupies 8 bytes, no matter what type of pointer it is. But the C compiler needs to know more about ...
Ralf Kleberhoff's user avatar
14 votes
Accepted

How important is memory alignment? Does it still matter?

Yes both alignment and arrangement of your data can make a big difference in performance, not just a few percent but few to many hundreds of a percent. Take this loop, two instructions matter if you ...
old_timer's user avatar
  • 969
13 votes
Accepted

Memory optimization of public methods in java

private methods can never be overridden, whereas protected and public methods can be overridden. As a consequence of this, the underlying runtime knows that for private methods: There is no need to ...
Erik Eidt's user avatar
  • 33.4k
12 votes
Accepted

C++ Dependency Injection vs Memory Usage

Well you're right about something being wrong. But I highly doubt worrying about memory usage is going to fix it. Unless you can point to some real world data that shows you have a memory problem at ...
candied_orange's user avatar
12 votes
Accepted

Is it possible to update exactly 1 byte in RAM?

The internet. When you have multiple computers networked together there is no such thing as "the word size". Every computer has its own idea how big it's bus is. But they all agree on bytes (even if ...
candied_orange's user avatar
11 votes

If I need to use a piece of memory throughout the lifespan of my program, is it really necessary to free it right before program termination?

Freeing memory at the end of a programs run is just a waste of CPU time. It's like tidying a house before nuking it from orbit. However sometimes what was a short running program can turn into part ...
Peter Green's user avatar
  • 2,165
11 votes

When to optimize for memory vs performance speed for a method?

I would optimize for readability. Method X: private bool IsSumInRange(int number1, int number2) { return IsValueInRange(number1+number2, -1000, 1000); } private bool IsValueInRange(int Value, ...
Pieter B's user avatar
  • 13.2k
9 votes
Accepted

Memory usage of JavaScript string type with identical values

The process of sharing strings in this manner is called String Interning, and yes, Javascript does it. The way in which string interning is accomplished in Javascript is an implementation detail, and ...
Robert Harvey's user avatar
9 votes
Accepted

Is there a drawback at allocating a huge amount of the stack for a single array in an embedded system?

The only thing I'm conscious is that I have to make sure I actually have 1KB of stack free when entering this function. Yes, and that is a strong constraint. You'll better be sure statically than you ...
Basile Starynkevitch's user avatar
9 votes

How often can we use garbarge collection in C#

In general, asking the garbage collector to collect is more of a recommendation than an actual order. You should not do this at all. The garbage collector will properly respond to memory pressure. ...
Telastyn's user avatar
  • 109k
9 votes
Accepted

What memory cleaner software exactly do?

I was wondering what exactly memory cleaner software do? In the best case, they do absolutely nothing. If you're lucky. Sometimes, they will actually free up RAM by deleting caches or forcing ...
Jörg W Mittag's user avatar
8 votes

How much stack usage is too much?

If you allocate an array of say 10,000 bytes on the stack, then that array is limited in size. 10,000 may be a lot, but if you need 10,001 bytes then your program can crash or worse. So in this ...
gnasher729's user avatar
  • 42.7k
8 votes
Accepted

Strategy to avoid running out of memory in memory intensive application

Unfortunately, there's really no good way to answer the question "how much RAM is my program using?" or "how close am I to hitting the OutOfMemoryException wall?", for a number of reasons. It's a lot ...
Mason Wheeler's user avatar
8 votes
Accepted

Why is accessing elements of a huge dynamically allocated structure a lot slower than a small dynamically allocated array in C++?

You have some very good answers on the topic here Generally, your struct is probably too big for the CPU cache, so probably parts of it end up in L2 cache or in RAM memory, which is significantly ...
Vladimir Stokic's user avatar
8 votes

Is it possible to update exactly 1 byte in RAM?

The C Standard requires that the implementation must be able to read and write single bytes. If you have an array char a[100] and one thread increases a[0] by 1, while another thread increases a[1] by ...
gnasher729's user avatar
  • 42.7k
7 votes
Accepted

Temporary Variables or Not: Choose CPU Time or RAM?

This optimization is called common subexpression elimination and is built into nearly all optimizers. The actual draw back of too many eliminations is register pressure, a.k.a. running out of ...
ratchet freak's user avatar
7 votes
Accepted

Is O(log n) for memory management considered slow?

The "ideal" performance of an algorithm in people's minds is dependent on what the best option is out there. If you can do a "find" operation on a data structure in O(n log N) time, is that fast ...
Cort Ammon's user avatar
  • 10.9k
7 votes
Accepted

Temporal logic as a programming language paradigm?

TLA+ isn't so much a general purpose language, but... One of the benefits of using TLA+ to investigate the bug is that once you can reproduce the bug, it’s easy to try alternative fixes. The bug in ...
Robert K. Bell's user avatar
6 votes

When was block scope for variables introduced to C, and why is it still frowned upon?

In the C Reference Manual that came with 6th Edition Unix (May 1975, second volume entitled "Documents for Use With the Unix Time-sharing System") the only compound statement allowed to contain ...
manlio's user avatar
  • 4,176

Only top scored, non community-wiki answers of a minimum length are eligible