Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you protect yourself from runaway memory consumption bringing down the PC?

Tags:

.net

windows

clr

Every now and again I find myself doing something moderately dumb that results in my program allocating all the memory it can get and then some.

This kind of thing used to cause the program to die fairly quickly with an "out of memory" error, but these days Windows will go out of its way to give this non-existent memory to the application, and in fact is apparently prepared to commit suicide doing so. Not literally of course, but it will starve itself of usable physical RAM so badly that even running the task manager will require half an hour of swapping (after all the runaway application is still allocating more and more memory all the time).

This doesn't happen too often, but when it does it's disastrous. I usually have to reset my machine, causing data loss from time to time and generally a lot of inconvenience.

Do you have any practical advice on making the consequences of such a mistake less dire? Perhaps some registry tweak to limit the max amount of virtual memory an app is allowed to allocate? Or some CLR flag that will limit this only for the current application? (It's usually in .NET that I do this to myself.)

("Don't run out of RAM" and "Buy more RAM" are no use - the former I have no control over, and the latter I've already done.)

like image 391
Roman Starkov Avatar asked Jun 11 '10 17:06

Roman Starkov


People also ask

Why is my PC using so much memory?

All computer memory is connected to the CPU and RAM. However, the high memory usage problem is mainly due to the overcrowding of many internal processes. Therefore, it helps to stop the unnecessary programs and applications that are running. Open the Task Manager and check any extra programs you aren't using.


1 Answers

You could keep a command prompt open whenever you run a risky app. Then, if it starts to get out of control, you don't have to wait for Task Manager to load, just use:

taskkill /F /FI "MEMUSAGE ge 2000000"

This will (in theory) force kill anything using more than 2GB of memory.

Use taskkill /? to get the full list of options it takes.

EDIT: Even better, run the command as a scheduled task every few minutes. Any process that starts to blow up will get zapped automatically.

like image 76
dmb Avatar answered Oct 11 '22 23:10

dmb