www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Request help on allocator.

reply Vino B <akashvino79 gmail.com> writes:
Hi All,

   Request your help in understanding the below program, with the 
below program I can allocate 8589934592(8GB) it prints the length 
8589934592(8GB) where as my laptop has only 4 GB so the confusion 
is that how can this program allocate 8GB RAM when I have only 
4GB of RAM installed


```
import std.algorithm.comparison : max;
import std.experimental.allocator.building_blocks.allocator_list 
: AllocatorList;
import std.experimental.allocator.building_blocks.bucketizer;
import std.experimental.allocator.building_blocks.free_list : 
FreeList;
import std.experimental.allocator.building_blocks.region : Region;
import std.experimental.allocator.building_blocks.segregator;
import std.experimental.allocator.common : unbounded;
import std.experimental.allocator.mallocator : Mallocator;
import std.stdio: writeln;

void main () {
alias FList = FreeList!(Mallocator, 0, unbounded);
alias A = Segregator!(
       8, FreeList!(Mallocator, 0, 8),
       128, Bucketizer!(FList, 1, 128, 16),
       256, Bucketizer!(FList, 129, 256, 32),
       512, Bucketizer!(FList, 257, 512, 64),
       1024, Bucketizer!(FList, 513, 1024, 128),
       2048, Bucketizer!(FList, 1025, 2048, 256),
       3584, Bucketizer!(FList, 2049, 3584, 512),
       4097 * 1024, AllocatorList!(n => Region!Mallocator(max(n, 
1024 * 4096))),
       Mallocator
);
A tuMalloc;
auto c = tuMalloc.allocate(8589934592);  // 8GB
writeln(c.length);                       // output: 8589934592
tuMalloc.deallocate(c);
}
```

From,
Vino
Dec 02 2023
next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Saturday, 2 December 2023 at 19:13:18 UTC, Vino B wrote:
 Hi All,

   Request your help in understanding the below program, with 
 the below program I can allocate 8589934592(8GB) it prints the 
 length 8589934592(8GB) where as my laptop has only 4 GB so the 
 confusion is that how can this program allocate 8GB RAM when I 
 have only 4GB of RAM installed


 ```
 import std.algorithm.comparison : max;
 import 
 std.experimental.allocator.building_blocks.allocator_list : 
 AllocatorList;
 import std.experimental.allocator.building_blocks.bucketizer;
 import std.experimental.allocator.building_blocks.free_list : 
 FreeList;
 import std.experimental.allocator.building_blocks.region : 
 Region;
 import std.experimental.allocator.building_blocks.segregator;
 import std.experimental.allocator.common : unbounded;
 import std.experimental.allocator.mallocator : Mallocator;
 import std.stdio: writeln;

 void main () {
 alias FList = FreeList!(Mallocator, 0, unbounded);
 alias A = Segregator!(
       8, FreeList!(Mallocator, 0, 8),
       128, Bucketizer!(FList, 1, 128, 16),
       256, Bucketizer!(FList, 129, 256, 32),
       512, Bucketizer!(FList, 257, 512, 64),
       1024, Bucketizer!(FList, 513, 1024, 128),
       2048, Bucketizer!(FList, 1025, 2048, 256),
       3584, Bucketizer!(FList, 2049, 3584, 512),
       4097 * 1024, AllocatorList!(n => Region!Mallocator(max(n, 
 1024 * 4096))),
       Mallocator
 );
 A tuMalloc;
 auto c = tuMalloc.allocate(8589934592);  // 8GB
 writeln(c.length);                       // output: 8589934592
 tuMalloc.deallocate(c);
 }
 ```

 From,
 Vino
This is normal behavior on linux, it's called overcommit memory, i think it was meant to allow things like fork() to work properly https://stackoverflow.com/a/7504354
Dec 02 2023
prev sibling parent IGotD- <nise nise.com> writes:
On Saturday, 2 December 2023 at 19:13:18 UTC, Vino B wrote:
 Hi All,

   Request your help in understanding the below program, with 
 the below program I can allocate 8589934592(8GB) it prints the 
 length 8589934592(8GB) where as my laptop has only 4 GB so the 
 confusion is that how can this program allocate 8GB RAM when I 
 have only 4GB of RAM installed
 From,
 Vino
Welcome to the wonderful world of virtual memory. Virtual memory size != physical memory size This nothing to do with D programming language but how the OS manage the memory.
Dec 02 2023