How to Consume Less Memory in C#Runtime?

Asked

Viewed 173 times

2

What I’d like to know is what are the most efficient ways to consume less memory in Runtime.

Use of variables "Static" ?

Create object with instants or a static class?

Use of the Dispose() to free memory, etc.

  • Just guessing, I suspect you’re correcting the wrong problem. Most applications don’t have memory problems unless they do things they shouldn’t.

  • In fact, I just wanted tips on how to program more efficiently with regard to the use of variables and methods

  • 1

    The Dispose does not release memory, at least should not, because it will depend on who implemented it. The Dispose should serve to release external resources and just that. About the Static, there may actually be a gain in some cases because there is no reference passage from the this for the method, but you need to consider that if you’re so concerned about this kind of thing, you should be using language more appropriate to the problem you’re having.

2 answers

7


The question begins a little wide. There are many ways and the subject interests me, I think it’s cool to use memory saving techniques because they help a lot in performance. Less memory used (in heap) brings less generated junk, less collections, less breaks, less overhead for memory management.

Use of static variables tend to waste more memory, or at least retain it for longer. If the semantics you need is this, ok use, otherwise do not use.

However, its use can be very useful, not to save, but to avoid unnecessary collections. If you know that there will be no concurrent access (in some cases it is possible, just need more care) and that the object changes sometimes, but always keeps an instance of it, it can be more interesting to create an object and reuse it, it can be a "poor" way of making a pool of objects. But you have to understand what you are doing. Done correctly you create fewer objects and generate fewer collections by taking fewer breaks. One of the difficulties is when the object has no set size, as the case of strings, another is that it can lose locality and the object stay away from its container, what can make everything slower, then starts to create the problem that languages that do not have GC have (contrary to popular belief, GC can offer better performance in many scenarios).

It doesn’t matter if the class is static or not. It itself does not define state, the only thing is that it may not have been in an instance of it, but it may have instances of other objects within it.

The Dispose() is interesting where it is needed, and has several questions about this (I think there’s more without the tag).

Pool of objects is a strong tip and . latest NET (2.1) already has this more or less ready for trivial situations. And has additional like the pool of streams, just to be in an example.

The use of ref in structs can bring a huge gain, but again, you need to know how to use right. See also What good is this 'in' in C#?.

The techniques are many. There are more specific things, there are things that depend on what you are doing, there are many things that are small details.

-1

Using Static is the worst thing you can do in terms of memory. By using Static you are telling the server that this variable is always available in memory because it starts as soon as the program starts.

What do I recommend?

Do not create variables to store values that will only be used once. Example:

int result = num1 + num2;
if(result<0)
{}

Can be summarized in

if((num1+num2)<0) {}

Another thing I recommend is using() so that all the variables used within this block are collected by Garbage Collector once the code leaves this block.

  • 3

    The last paragraph makes no sense. The use of Dispose should have absolutely nothing to do with garbage collection, it should be used for releasing external resources. If someone implements Dispose forcing the collection, you’re doing it completely wrong. Even, forcing garbage collection is one of the worst things you can do when you want to take care of your memory.

  • 3

    Use static is far from being the worst thing you can do with memory, in fact there are cases that is the best. Server?!? Your example saves zero in memory. I don’t think you understand the functionality of using and also see @LINQ comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.