Is there a way to limit the memory a process uses in Win7 x64?

11,174

Solution 1

There is the Windows System Resource Manager, but this only applies to Windows Server 2008.

In general, this is NOT a good idea--the page file is meant to be used. Most processes don't know the limit of their physical memory, nor should they be required to care. It sounds like you should clean up programs using the resources, or invest in more memory--4GB is pretty standard nowadays, and more is better.

Solution 2

  • On Win7, open msconfig, then Boot panel => Advanced options => check Maximum memory and set the limit in the textbox below the checkbox => Restart. This limits the total memory usage.

  • Another way could be running a batch script in the background, monitoring the memory usages of processes and kill it if it eats too much memory.

I had a unwanted process (it downloads lots of Ads) consuming too much memory (always started by another program I use frequently). Here is the bat script I monitor and close it automatically.

@echo off
set prog="XXXX.exe"
:1
SET found=1 
(tasklist /FI "username eq <your username>" | find %prog% || set found=0) >nul 2>nul
if %found% EQU 1 (taskkill /f /im %prog%)
ping 0.0.0.1 -n 600 >nul 2>nul
goto 1

May someone can extend it to monitor any process.

  • If you wrote your own program, you can utilize the Job objects API

    A job can enforce limits such as working set size, process priority, and end-of-job time limit on each process that is associated with the job. If a process associated with a job attempts to increase its working set size or process priority from the limit established by the job, the function calls succeed but are silently ignored. A job can also set limits that trigger a notification when they are exceeded but allow the job to continue to run.

Solution 3

It appears that there is no native way to do this, as yet again, M$ treats its users like incompetent idiots that shouldn't be allowed to perform tasks that might actually make the entire system more stable or productive.

The only workaround might be what was suggested for running a batch script in background (technically that falls under 3rd party, as the script is written and run unofficially by another party), but this wouldn't "limit" the process per se, but would instead restart the process when it hits its max limit you impose.

The issues with a restarting script that you should consider are:

  1. You will be killing the process unexpectedly, which means it will be interrupted in whatever task it was performing.

  2. Corrupted files could be generated if a file were being written to when the process gets restarted. This can be alleviated if the process has its own file verification and rewrite contingencies (such as online games and Steam).

  3. Other sub-processes and threads will also be killed as a result, and could propagate the other issues listed here.

  4. If the process eats up RAM rather quickly, restarting the process might occur too often, rendering the process useless (file a critical bug report and stop using the software).

  5. Although the Software process is experiencing an unintentional memory leak, it is possible that the process has an internal policy of behavior when the system is low/depleted in RAM, such as when it ends up using the Pagefile. If it changes its behavior to reduce its footprint in this case, restarting the process ensures it will never enter this kind of "reduced" state of operation.

Another reason why the Linux Team got things right, but alas, Windows got the world's support in drivers and games first, so we have to outlive it.

BTW, I personally experience a MASSIVE memory leak in the Razer GameScannerService.exe process, where of my 16GB of 1800MHz memory (and my 5GHz CPU), within a few minutes it has already allocated nearly 2GB of RAM, and I have seen it climb all the way up to 9GB if I allowed it to.

Razer does nothing to fix this, despite the massive number of bug reports. Our only option is to disable the service in some way and live without our ability to have Razer auto-apply profiles to games or auto-discover new games that get installed.

Share:
11,174

Related videos on Youtube

macjayz
Author by

macjayz

Updated on September 17, 2022

Comments

  • macjayz
    macjayz over 1 year

    I think Linux can do this using ulimit, I'm looking for something similar in Win7 x64. The problem is some times a process doesn't know the physical memory is already exhausted and continues to allocate memory, which will slowdown the system to a crawl since system is allocating from pagefile. The only way to recover is hard reboot.

    Thanks

  • macjayz
    macjayz over 13 years
    Yeah, I know this is unusual, but unfortunately in this case it's necessary since the process will consume all system resources and lock up the machine
  • will
    will over 8 years
    It isn't always a practical. Recently Firefox 64-bit has been burning the physical memory like it is going out of fashion. I have to exit Firefox now to run some business related processes. Having a memory quota on programs is a necessary thing in some cases.
  • Gordon Bean
    Gordon Bean over 8 years
    This question isn't about poor programming and buying more RAM - in my opinion is is about a critical issue with operating system design. I run into the OP's scenario while performing interactive data analysis (MATLAB, R, IPython, etc.) You load a large data set and start working. Then you inadvertently (through a typo, api misunderstanding, whatever) call some function that squares your data. Instead of taking 8 GB, it now takes 64. Your computer hangs because it's trying to allocate 64GB and the operating system has nothing left.
  • MDMarra
    MDMarra over 7 years
    That will set the maximum total memory that can be consumed. OP was looking for the ability to limit the memory consumption of individual processes.
  • ddzzbbwwmm
    ddzzbbwwmm over 7 years
    @MDMarra I don't think it's a conflict. Think from another side, when limiting the total memory, single process is also limited. It appears the only way on Win7 without 3rd-party tool since Windows System Resource Manager was not introduced until Win8.
  • MDMarra
    MDMarra over 7 years
    They're two very different things. If you limit a single process, the rest of the memory is available to all other processes. If you cap all processes and one process hits the cap, the rest are also impacted.
  • ddzzbbwwmm
    ddzzbbwwmm over 7 years
    Agree, if more than one process consume lots of memory are running. I was thinking only one process making trouble. I proposed another possible way (but not totally a complete solution for the needs of the OP).
  • jpaugh
    jpaugh over 6 years
    There are apps which will take more memory it if's available, causing others to suffer. FF works fine on my 8 Gb system, but takes a huge chunk of memory on my 20Gb system. Chrome acts similarly.
  • jpaugh
    jpaugh over 6 years
    This is perfect for web browsers, which insist on caching a huge amount of stuff in memory, but recover well from unexpected shutdowns. Do you know of such a script?