The Mask
The Mask

Reputation: 17427

variables in C# - unset after function execution?

is a good method, I unset variables after function execution?

Well,I need better performance for my C# application I need to consume less memory ram possible's

Upvotes: 3

Views: 684

Answers (5)

Manish Basantani
Manish Basantani

Reputation: 17509

As pointed out by others, you should be concerned about: 1. Calling Dispose of IDisposable objects. 2. Unsubscribing the handlers (if any).

Other than this, there should not be a reason to worry about optimization unless some memory profiler raises alarm.

Upvotes: 1

Brennan Vincent
Brennan Vincent

Reputation: 10665

Variables of value types (most built-in types like int, char, bool, as well as structs) will normally have their memory reclaimed immediately when a function returns, since they are allocated "on the stack" (which means they are part of the data structure that gets created when a function begins execution, and is freed when the function returns).

Variables of reference types will be automatically freed by the garbage collector when they are no longer needed.

Memory not being freed when functions return is unlikely to be what is causing problems with your program.

As another answer stated, do not optimize unless you have hard evidence of where the problem lies.

Upvotes: 9

TrueWill
TrueWill

Reputation: 25543

In general the only "management" you have to concern yourself with in C# is disposing of objects whose classes implement IDisposable. This is generally done through using blocks.

Once in a while you have to remember to unsubscribe from an event.

Otherwise, don't worry about it (unless you're allocating tens of thousands of objects and keeping them in memory). Trust the garbage collector.

Upvotes: 2

tbddeveloper
tbddeveloper

Reputation: 2447

If you're looking for problems in your software, there is a free profiler, XTE Profiler. This will allow you to find any memory leaks or bottlenecks in your code. Also, right now, Telerik has released their own memory profiler, it's currently available for free but it's in beta.

Upvotes: 3

Steve Wellens
Steve Wellens

Reputation: 20640

is a good method, I unset variables after function execution?

Nope, that is a waste of time. Do not optimize until you KNOW where the slowness is.

Upvotes: 11

Related Questions