1

I'm working on an application, which has the tendency to use excessive amounts of memory, so I'd like to reduce this.

I know this is possible for a Java program, by adding a Maximum heap size parameter during startup of the Java program (e.g. java.exe ... -Xmx4g), but here I'm dealing with an executable on a Windows-10 system, so this is not applicable.

The title of this post refers to this URL, which mentions a way to do this, but which also states:

Maximum Working Set. Indicates the maximum amount of working set assigned to the process. However, this number is ignored by Windows unless a hard limit has been configured for the process by a resource management application.

Meanwhile I can confirm that the following lines of code indeed don't have any impact on the memory usage of my program:

HANDLE h_jobObject = CreateJobObject(NULL, L"Jobobject");
if (!AssignProcessToJobObject(h_jobObject, OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId())))
{
    throw "COULD NOT ASSIGN SELF TO JOB OBJECT!:";
}
JOBOBJECT_EXTENDED_LIMIT_INFORMATION tagJobBase = { 0 };
tagJobBase.BasicLimitInformation.MaximumWorkingSetSize = 1; // far too small, just to see what happens

BOOL bSuc = SetInformationJobObject(h_jobObject, JobObjectExtendedLimitInformation, (LPVOID)&tagJobBase, sizeof(tagJobBase));

=> bSuc is true, or is there anything else I should expect?

In top of this, the mentioned tools (resource managed applications, like Hyper-V) seem not to work on my Windows-10 system.

Next to this, there seems to be another post about this subject "Is there any way to force the WorkingSet of a process to be 1GB in C++?", but here the results seem to be negative too.

For a good understanding: I'm working in C++, so the solution, proposed in this URL are not applicable.

So now I'm stuck with the simple question: is there a way, implementable in C++, to limit the memory usage of the current process, running on Windows-10?

Does anybody have an idea?
Thanks in advance


  • wouldnt it be better to fix the cause of the problem instead of fighting the symptoms? - user463035818
  • "I'm working on an application, which has the tendency to use excessive amounts of memory, so I'd like to reduce this." - then use less memory. Any other method will result in your application failing to allocate memory (and likely dying shortly afterwards) which is probably not what you want. - UKMonkey
  • Answering the first comments : the process is multi-threaded and handling queues. The idea is that, when I see that the amount of memory usage becomes too large, a thread might decide to wait a bit for handling its queues until the memory usage becomes reasonable again. - Dominique
  • No. If the was an OS/C++ option to restrict memory all that would happen is that your program would crash with an out-of-memory exception. As @user463035818 says you need to fix the underlying problem. - Richard Critten
  • threads dont decide to wait based on the available memory. Either there is enough memory or you run out of memory and in that case you will have more severe problems than threads waiting to handle queues ;) - user463035818

Linked


Related

Latest