Click here to Skip to main content
15,882,721 members
Please Sign up or sign in to vote.
1.39/5 (4 votes)
See more:
Hello Friends,

I have created a C# console application which I need to run on 24/7 basis to continuously perform database requests and do heavy calculations on the data inserted. It works fine for 24 hours, but after that it gives memory full exception.

I really need solution for this.

Please suggest a solution, any help will be appreciated.

Updated 16-Aug-16 1:26am
jim lahey 11-Jan-11 11:00am    
without seeing your code I can't possibly help fix the issue.

You could set up a scheduled task to stop and restart your console application every 23 hours I suppose.
Jagjot Singh 11-Jan-11 11:08am    
Here is the flow of the code:

Loop (infinite)
get values from database;
perform calculations;
store the values to the database;
Yusuf 11-Jan-11 11:09am    
smells like a resource leak to me. Check your resource utilization. Clean up [Delete, free] any unwanted resource.

Make sure you Dispose of your SqlConnection and SqlCommand objects.
I assume you use them.
Any unmanaged code?

Share this answer
Jagjot Singh 11-Jan-11 11:06am    
I am disposing the SqlConnection and SqlCommand objects. The thing is that the application is running in infinite loop. So in looping it is difficult to free the resources.
Sergey Alexandrovich Kryukov 11-Jan-11 11:22am    
@Jagjot: Difficult in looping? No, it is not. It's difficult with no looping, because it's harder to detect a problem.
Sergey Alexandrovich Kryukov 11-Jan-11 11:25am    
@Jagjot: perhaps you need to show more detailed code. You certainly do something else.
Sergey Alexandrovich Kryukov 11-Jan-11 11:26am    
@Estys: My 5, but... one more idea in my answer.
Jagjot Singh 11-Jan-11 11:31am    
no unmanaged code at all.
You've either got a memory leak somewhere (doubtful, but possible), or a handle leak. Both will give you the Out Of Memory error.

You can see if you've got a handle leak by opening TaskManager, go to View, Select Columns..., and turn on Handles. Start your process and let it run through a few iterations. Jot down the numbers you get. Then come back in a few hours and compare the numbers. If the handles numbers are considerably higher, you're not Disposing an object than you should be.

I'd consider this a definite possiblity if your process exceeds 5,000 to 8,000 handles. I've seen bad run-away processes consume over 100,000 handles (very bad!) before Windows starts to do very strange things.

It may also not be YOUR process that's causing the problem, so look at everything!

This error happens quite frequently when newbies try to do their own drawing in Windows Forms app without knowing that they have to Dispose every GDI object they create, commonly Pens and Brushes. They end up creating a new Pen or Brush on every call of their Paint event, never disposing the previous ones, and eventually they get an Out Of Memory error.
Share this answer
Sergey Alexandrovich Kryukov 12-Jan-11 15:10pm    
Useful directions - my 5
Jagjot Singh 13-Jan-11 0:15am    
Very useful instructions, I found that number of handles for my application is increasing with the time. I believe that I am not disposing the objects properly.
I agree withe the Answer by Estys, but you say it did not work for you -- yet. So, in addition to that:

You should not assume that a memory leak is not possible only because a garbage collection works. Many people allow the leak by holding garbage collector. Your application design is the one prone to this problem. For (one typical) example, if you improve performance of calculations by adding some intermediate values found in database to some dictionary or other container, it will hold references if you forget to clean up the container. (You may need to learn about weak references as well.)

So check up you calculation part as well.
Ask yourself: what are the variables accessed outside the loop. Check up; get rid of them as much as possible.
Also, gather some memory consumption statistics through the loop cycles.
Share this answer
Jagjot Singh 11-Jan-11 11:41am    
I dint say that I am not agree with Estys. If application is giving "out of memory" exception, it certainly means that something is really wrong. I just said that application is having a loop and it is fully written using No unmanaged code is there like win 32 dlls etc. I'll try to make the changes to my code and try fix the issues as per your suggestions.
Sergey Alexandrovich Kryukov 11-Jan-11 13:11pm    
>" I dint say that I am not agree with Estys."
Jagjot, nobody blamed you for doing anything wrong or not understanding Estys. I only suggest another possible way of having the leak. My point is different: managed code can cause leak as well (despite of common misconception, it can). Please read carefully and try to check up.
Jagjot Singh 11-Jan-11 19:58pm    
how we can find the memory leaks?. I am sorry if someone hurt with my comments.
Sergey Alexandrovich Kryukov 12-Jan-11 0:38am    
I never used special tools with .NET (but used extensively for native Windows programming) and had very few memory problem and easily resolved them manually (believe not not), so I just know there are some tools I never used.

This is CodeProject work:

Also, I just Googled:

leak ".net" detection

I gave me almost a million results, top of the list was of quite good relevance, 3 commercial tools (hopefully, free trial) + a lot more.

(Jagjot,.. I don't think you hurt anyone's feeling or can be blamed for anything, don't be so suspicious; I'm telling you with open heart, with no sarcasm.)
You need to read up on memory managment in .Net, namely, about the large heap.[^]

For more info, run this google search - ".net large heap"
Share this answer
Also I think a windows service would be the appropriate choice for running all the time.
Share this answer
Share this answer
Richard Deeming 16-Aug-16 9:09am    
This question was asked, answered, and solved over FIVE YEARS AGO!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900