Why SetProcessWorkingSetSize() fails when called second time

I try to allocate as much as possible physical RAM on 2 GB notebook.

So I:

1. Determine how much memory can I use.

mem_size = phys_mem_size - commited bytes ("Commited Bytes" of "Memory" object - performance counter)

2. Call SetProcessWorkingSetSize() with original limits increased by mem_size.
SetProcessWorkingSetSize() succeeds.

3. Call VirtualAlloc() with "commit" flag starting from mem_size.
After some iterations (decreasing size) function succeeds.

4. Call VirtualLock() to force pages being in RAM. It succeeds also.

5. Call VirtualUnlock()

6. Call VirtualFree()

7. Call SetProcessWorkingSetSize() with original limits.

8. SetProcessWorkingSetSize() again and this time it fails.

Let's say that when called for the first time it succeeded for 700 MB, but for the second time succeeded for 70 MB. Why ?

Unterforunately I do not have acceess to this laptop (it is over the ocean) so I cannot debug it.
I never noticed such problem eralier on 512 MB PC (W2K) nor my current 256 MB PC (XP).
Dariusz DziaraProgrammerAsked:
Who is Participating?
CetusMODConnect With a Mentor Commented:
PAQed with points refunded (500)

Community Support Moderator
After step 6, you might need to check how much memory you have. The free process might take some time.
Please have a look at this link, it may help


Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

What are the parameters that you pass to SetProcessWorkingSetSize  when it fails.
Dariusz DziaraProgrammerAuthor Commented:
What are the parameters that you pass to SetProcessWorkingSetSize()

MIN = ORG_MIN + estimated_mem_size + 30 MB
MAX= ORG_MAX + estimated_mem_size + 30 MB


estimated_mem_size = total_phys_mem_size - commited_bytes - 70 MB

Memory is allocated by application process (Admin user).

Process privilege SE_INC_BASE_PRIORITY_NAME is enabled (AdjustTokenPrivileges() with SE_PRIVELEGE_ENABLED).

The same parameters (if I am not mistaken) are used for the first time when SetProcessWorkingSetSize() succeeds and for the second time when it fails.

After step 8 steps are repeated starting from step 1 (on interactive request).

"estimated_mem_size" in second iteration is similar but just SetProcessWorkingSetSize() fails.
Dariusz DziaraProgrammerAuthor Commented:
I should say steps 1-7 step 8 is just element of next 1-7 steps
I think the only way when SetProcessWorkingSetSize fail is when MIN = MAX. this may happen if estimated_mem_size is negative i.e total_physical_mem_size - commited_bytes is less than 70 MB.
Dariusz DziaraProgrammerAuthor Commented:
No, I test if total_physical_mem_size > commited_bytes + 70 MB
It cannot be negative. Apart from that I use unsigned variables and also should't it happen in the first iteration also ?
Dariusz DziaraProgrammerAuthor Commented:
I have latest news:
  After removing 1 GB memory module from notebook all works fine.
It's usually a poor idea to set the working set size.   The system or some other app or service may try to start up and need some memory, and there may not be any available since you're hogging it.

Normally Windows does a good job of allocating memory-- why not let it do it's thing?

Dariusz DziaraProgrammerAuthor Commented:
"It's usually a poor idea to set the working set size" - I know.
In the first place I wanted it to work least harmfull way possible - to correctly estimate amount of memory I can alloc & lock without interferring currently running software (system & applications).
I want it to be in physical memory before this memory is to be written in round-robin manner sometimes for long hours till some event will happen. Hence I don't want some memory pages to be page out to hard disk (saving disk & performance also are issues).
By the way what can limit parameters passed to SetProcessWorkingSetSize() ?
I know that VirtualAlloc() requires contiguous range of addresses in virtual address space. I am not sure if memory can be compacted some way by Memory Manager.
Dariusz DziaraProgrammerAuthor Commented:
It should be:

"I want it to be in physical memory because"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.