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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
memory-usage × 97memory × 36
c × 16
c++ × 14
optimization × 8
garbage-collection × 8
c# × 7
performance × 7
java × 6
heap × 4
speed × 4
algorithms × 3
javascript × 3
programming-practices × 3
data-structures × 3
operating-systems × 3
storage × 3
cpu × 3
stack × 3
allocation × 3
design-patterns × 2
object-oriented × 2
database × 2
python × 2
.net × 2