tags:

views:

446

answers:

5

Is there a way to just clear all memory in c sharp ?

For example my program is being run one time and it has filled an array.

Is there a function which could clean all memory before the 2nd run so that I do not need to clear all variables ?

Edit: Including text of OP's answer below

I am using libcurl in my program. I click run, libcurl fetches one page and program stops. When I click run button again (Without stopping to debug) libcurl never fetch any webpage again. Using libcurl in made functions for clearing memory does not work so I taught why not clear all memory if possible.

If I restart program then webpage is being fetched successfully first time. I actually stop everything - All threads, clear all variables but nothing works

GC.Collect() did not help either. Looking for solution

+2  A: 

C# uses a garbage collector, which clears all the unused objects.

the GC runs once in a while, but you can call it directly:

GC.Collect();

Note

As others pointed out, this is not recommended.

Am
Calling the Garbage Collector manually can "promote" some variables into long term memory and actually prevent them from getting cleared at the appropriate time. 99% of the time its best to leave the GC alone and let it clear up memory for you.
Michael La Voie
Doesn't do what was asked. The OP didn't ask about freeing objects; the OP asked about "cleaning memory" related to an array.
Ken White
Actually the question was about clearing memory, and the array was an example. To free up memory of a C# app you use the GC.
Am
@Am: Well, freeing objects doesn't clear the memory and the whole idea of automatic garbage collection is not to do it manually.
Brian Rasmussen
I agree, and I don't recommend doing it. Even the need of using this points to a design error in your code.
Am
+4  A: 

After running an application, remember to hold the computer upside-down and shake it. This will ensure any allocated memory has been cleared.

John Millikin
+0.5 for chuckle and +0.5 for setting it as community wiki
Daniel
-1. It did make me chuckle, but this is in pretty poor taste, I would think...
Daniel Pryden
-1 for making an obviously snide remark a CW so you don't have to take downvotes.
Matthew Jones
+1 This wasn't cutting down the question, and it was understandably made CW because there are people who take sort of answer this too seriously.
Noldorin
A: 

When your operating system executes a process (written in C# or any other programming language), the process allocates resources from the operating system. Memory (RAM) is one such resource. The operating system keeps track of what memory your process has requested, and when your process exits (that is, when your program is finished running), the operating system reclaims all the memory.

In practice, the operating system is probably using virtual memory, page fault traps, and memory-mapped pagefiles to make all this happen, but you shouldn't have to worry about that -- that's the operating system's job.

Additionally, once you've requested memory from the operating system, while your program is still running, you can give it back if you're not using it anymore. Different languages have different mechanisms for this. In C# you have a garbage collector which finds any objects that are not referenced anymore and gives them back to the operating system. Again, you should rarely be concerned with this, since it's the .NET runtime's job to make sure this works.

Daniel Pryden
+5  A: 

In .Net languages like C# you should almost never call the Garbage Collector manually.

Though as @AM said

GC.Collect();
GC.WaitForPendingFinalizers();

Will call it, calling the Garbage Collector manually can "promote" some variables into long term memory and actually prevent them from getting cleared at the appropriate time. 99% of the time its best to leave the GC alone and let it clear up memory for you.

Please elaborate in your question why you want to do this. There are valid reasons, and if yours is one, then you'll get better answers by providing it. Otherwise, consider the option that you don't need to free the memory manually.

Why you shouldn't call the Garbage Collector

EDIT

You mentioned that the garbage collector isn't working for you. Please elaborate on why you think this is a memory related bug. Your clarification leads me to believe that you may be referencing the libcurl library which was written in C. If this is the case, then the Garbage Collector has no knowledge of the memory in that assembly.

In fact, if there is memory that needs to be freed from the assembly, it is either up to the assembly to do it by itself, or if they provided a public method that lets you instruct it to free memory, you could potentially call said method (but that's a big if).

Michael La Voie
+1 for C# != C in terms of memory management.
Matthew Jones
While this is technically correct, it does not actually answer the OP's question.
Daniel Pryden
A: 

I am using libcurl in my program. I click run, libcurl fetches one page and program stops. When I click run button again (Without stopping to debug) libcurl never fetch any webpage again. Using libcurl in made functions for clearing memory does not work so I taught why not clear all memory if possible.

If I restart program then webpage is being fetched successfully first time. I actually stop everything - All threads, clear all variables but nothing works

GC.Collect() did not help either. Looking for solution

Raimis
Instead of adding an *answer* to your question, please *edit* your original question to include the new information.
Daniel Pryden
FYI: I've copied this text into the question for you. You can delete this answer now.
Daniel Pryden