2

I write a c# console application. It may consume a large volume of memory. However, to guarantee it will not use up all my memory, I would like to set a maximum memory for it, say, 5GB. It should stop running or do some garbage collecting once it reaches the maximum memory.

The problem is, where could I set this constraint in visual studio? I guess there must be something configurable in App.config.

Jianxun Lian
  • 137
  • 1
  • 2
  • 5
  • see: http://stackoverflow.com/questions/1268095/setting-c-sharp-memory-parameters?rq=1 Seems like the exact same question. – Ben Aug 06 '14 at 08:01
  • 1
    @Andy No, that question is more to do with setting a minimum amount of memory (and, in fact, the reason for the question is nothing to do with that anyway!) – RB. Aug 06 '14 at 08:02
  • @Jianxun If .NET recieves a low-memory notification from Windows it will automatically force a garbage-collection. Please can you clarify what specific problem you face? – RB. Aug 06 '14 at 08:07
  • 1
    If your program is in .Net, I don't see why you would want to do that. .Net memory management is pretty lazy: if there is memory available, the GC will not bother freeing memory. But it will kick in as soon as memory becomes low. That means your program will never consume all the memory of your system, unless under very particular circunstances (unmanaged memory use for instance), or it would mean it actually NEED it, and then you have a completely different problem at hand. – Falanwe Aug 06 '14 at 08:07
  • Instead of trying to restrict the amount of memory that your application will utilize, have you tried profiling your application to see if there are ways of optimizing its operation and potentially reduce the memory required? – Jamie Keeling Aug 06 '14 at 08:08
  • this is a tool that might help: http://lowleveldesign.wordpress.com/2013/11/21/set-process-memory-limit-with-process-governor/ from here http://stackoverflow.com/questions/8966639/set-maximum-memory-usage-c-sharp – thumbmunkeys Aug 06 '14 at 08:11
  • possible duplicate of [Limiting memory usage of a .net application](http://stackoverflow.com/questions/5956556/limiting-memory-usage-of-a-net-application) – thumbmunkeys Aug 06 '14 at 08:18
  • My scenario is: I need load raw log data into my analytics model, however I already know this model is memory-consuming, and I want to do an experiment to know how much raw data I can process at one iteration, one hour, one day or one week? while doing this experiment, I am afraid if it eats up all the memory, the other running applications or back-end service will be affected. – Jianxun Lian Aug 06 '14 at 08:18
  • @JianxunLian Then you will probably need to profile it on a machine that doesn't have other running applications on it. In fact - the other running applications memory usage will have an impact on the amount of memory your app can claim without system instability, so while it might be fine up to 5GB at 7AM when nothing else is running much, it might only be fine to 3GB at 2PM when all the other users come back from lunch, for example. – RB. Aug 06 '14 at 08:28
  • @RB I agree. However the model may need 20~50 GB memory so I have to test it on a server... I know I could do some calculation and predict the result of one week from the result of one day, but it is not precise. So I am seeking if .NET would do memory limitation easily. – Jianxun Lian Aug 06 '14 at 08:45
  • I don't think there is a managed interface for [CreateMemoryResourceNotification](http://msdn.microsoft.com/en-us/library/windows/desktop/aa366541(v=vs.85).aspx) but it gives you on object that you can wait on in a thread. As soon as it gets signaled for low memory you could take action, for example stop your processing or stop loading stuff. That seems a nicer solution than the iteration/timer stuff.... – rene Aug 06 '14 at 08:50

1 Answers1

0

You can use Garbage Collector methods to find amount of currently used memory and then deal with it. I am afraid there is no option to set hard memory limit in VS.

Take a look at this question: Limiting the use of RAM. (C# .NET)

Community
  • 1
  • 1