I'm creating a server (Console App), but after doing some long-term testing I found out it grows eating RAM. For the local test suite, I am not working with much RAM (8GB-DDR3 @2400MHz)Is there a way (In Program.cs, I assume) to restart the program if it is using over 'x' amount of RAM? Also, one way could be a timed loop/checkup?
Asked
Active
Viewed 1,528 times
0
-
If you're writing a server, you usually don't want a console app, you want a Windows service. – svick Oct 16 '13 at 01:07
-
4Also, if the amount of consumed memory actually grows, you have a memory leak in your application that you should fix. Note that it may look like the consumed memory grows even when it doesn't. .Net doesn't always return free memory to the OS (unless the OS tells it that the memory is needed). – svick Oct 16 '13 at 01:09
-
2I suggest to check why your application uses that much RAM first instead of checking how much ram it used.. – User2012384 Oct 16 '13 at 01:09
2 Answers
0
You can use GC.GetTotalMemory
. It returns an approximate value (long
) of how much memory your program has allocated.
You can create a Timer
object and make this comparison under the Tick
event handler.
For more information, you can look here: http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx

Rohan
- 359
- 2
- 16
0
I agree with what others have said about fixing your memory leak.
If you want to restart your program, create a second application that monitors the first process. Then, when memory gets too high in your original app, safely shut it down and allow the second application to launch it again.

crthompson
- 15,653
- 6
- 58
- 80