147 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 ...
user avatar
  • 1,549
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 ...
user avatar
  • 184k
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 ...
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 ...
user avatar
  • 184k
41 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 ...
user avatar
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 ...
user avatar
  • 1,958
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 ...
user avatar
  • 2,912
20 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 ...
user avatar
  • 19.9k
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 ...
user avatar
  • 24.6k
15 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 ...
user avatar
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 ...
user avatar
14 votes

What is the point of using lists over vectors, in C++?

The only place where I usually use lists is where I need to erase elements and not invalidate iterators. std::vector invalidates all iterators on insert and erase. std::list guarantees that iterators ...
user avatar
  • 287
14 votes
Accepted

Why is it called a memory leak?

Look at it at the other way, from the system perspective. You have a giant pool of free memory (free ram memory) where different programs can make use of. But all of their used resources should be ...
user avatar
14 votes
Accepted

How to share memory between applications written in C/C++

But I cannot get my head around to how the boost library does this. The boost interprocess mechanism has three necessary components to work: memory-mapped file: a memory-mapped file needs to be ...
user avatar
  • 5,225
13 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 ...
user avatar
  • 949
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 ...
user avatar
  • 32.3k
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 ...
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 ...
user avatar
11 votes

Optimizing code by using registers, but what if they are not enough in number?

Having more variables than registers isn't necessarily a problem. If a variable's value isn't used after a certain point in the function, the compiler can use that register for another variable. Even ...
user avatar
  • 15.2k
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 ...
user avatar
  • 1,935
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, ...
user avatar
  • 12.4k
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 ...
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 ...
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. ...
user avatar
  • 107k
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 ...
user avatar
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 ...
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 ...
user avatar
  • 37.4k
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 ...
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 ...
user avatar
  • 37.4k
7 votes

Understanding how variable assignment works

This is a very good question -- to ask your instructor. She no doubt has a plan (or syllabus) of what she's going to teach and the order she's going to teach it in. The first example you show with ...
user avatar

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