Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I am trying to find a way to allocate physical not virtual memory in C#. I tried googling it and on Stack Overflow but found only virtual memory allocation which didn't work and didn't appear in Task Manager

What I have tried:

Stack overflow (only found virtual allocation)
Google, found nothing
Marshal.AllocHGlobal(bytes); // virtual memory allocation (not what i am looking for)

AllocateUserPhysicalPages(); // gave errors
Posted
Updated 17-Dec-21 21:41pm

Yes, AllocHGlobal allocates virtual memory by calling LocalAlloc (from winbase.h) with option LMEM_FIXED. It does not initialize the buffer whatsoever, which means no physical memory needs to be spent yet. As soon as you start using the memory, it will become physical memory.

Now I wonder why you would want unmanaged physical memory in the first place. Can you explain what your app is supposed to do with it? Maybe there are other and better ways to reach your goals...
 
Share this answer
 
Comments
j ongamer13 17-Dec-21 22:17pm    
I just want to waste the computer's RAM lol (without a forkbomb)
j ongamer13 17-Dec-21 23:09pm    
So it basically just does nothing with it and just waits until the user lets the RAM go.
Quote:
AllocateUserPhysicalPages(); // gave errors

Unless you tell us what the errors are we cannot help. You may like to check AllocateUserPhysicalPages function (memoryapi.h) - Win32 apps | Microsoft Docs[^] for further information.
 
Share this answer
 
Comments
j ongamer13 18-Dec-21 7:26am    
It gave a "AllocateUserPhysicalPages(); does not exist in the current context" error.
Richard MacCutchan 18-Dec-21 7:38am    
Yes, because it is part of the Win32 library, so you can only call it through P/Invoke.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900