-4

is there any option to lock or allocate memory in C#?

Scenario 1: In my virtual machine, there is 16GB RAM, for a test I need to use 8GB RAM so 8GB will remain 'free' for the operating system and rest application

Scenario 2: The same virtual machine with 16GB RAM, and now I need to use 14GB RAM.

For now, I create a memory leak function but this is not good cause it takes all memory from my virtual machine.

List<byte[]> memoryUsage = new List<byte[]>();
while (true)
{
    try
    {
        memoryUsage.Add(new byte[1024]);
    }
    catch (OutOfMemoryException)
    {
        throw;
    }
}

Allocate or Lock RAM that User (me) will input to file e.g I want to allocate/lock 8GB RAM and program allocate/lock 8GB RAM and 8GB RAM will remain as 'free'

Prochu1991
  • 443
  • 5
  • 20
Solaire
  • 23
  • 5
  • 1
    _"it takes all memory from my virtual machine"_ -- though, apparently only because you don't do anything to limit how much memory it allocates. Why not allocate less memory? C# can allocate memory, just as you do above. With unsafe code, you can pin (or "lock") it. With p/invoke, you can call system APIs to manage memory independently of the CLR-based code. But your question is unclear. Do you want to be able to use the "allocated"/"locked" memory? Or just consume it so it's not available to other things? What's the actual _goal_ here? – Peter Duniho Jul 29 '19 at 19:55
  • Just consume that memory so it won't be available to other things. – Solaire Jul 29 '19 at 20:13

1 Answers1

0

If you're running this on a Windows box, you need to keep in mind the concept of virtual memory. You can allocate - or leak memory - to your heart's content with C#, however if the underlying operating system deems safe to page the memory being used by your process to the paging file (assuming at least one such file is defined), it will do so.

Let's take your first scenario - the one where you want 8 GB of RAM allocated. Your code can do precisely that. However your OS can kick in and move some of the pages representing your allocated data in RAM to disk. There are several reasons why the memory manager would do this - take a look at some here (under the "Pages can be removed from a process working set..." paragraph). You'll thus be left with less used RAM that you originally intended.

To my understanding you're after a constant working set occupied by your process, and I'm not sure C# - even in an unsafe context - allows you to do that. You could try invoking Win32 functions that work at a low level, possibly using P/Invoke as stated here, but like I've said, I'm not sure it will work.

You'll also have to keep an eye out on the variable(s) that reference your allocated data. If the GC (Garbage Collector) decides the data you've allocated is no longer required from a point on (say because your remaining code no longer references it) it will happily reclaim it, again leaving you with less allocated memory that you've originally wanted.

You haven't really said anything about the target platform you've used to build this. An out-of-memory error will be thrown much earlier - after allocating only around 3 GB - by a process that was built for x86 (or AnyCPU + Prefer 32-bit), since that's how things work in Wow64.

If you don't really have to write the code that's doing the allocation yourself in C#, maybe you can merely invoke something like Testlimit (and play around with setting the minimum working set).

Mihai Albert
  • 1,288
  • 1
  • 12
  • 27