开发者

Hello world - what is a simple program to use 16GB of memory?

开发者 https://www.devze.com 2023-02-22 09:26 出处:网络
How can I allocate a large amount of memory with 16GB ram? Please provide a simple C/C++ program as an example.

How can I allocate a large amount of memory with 16GB ram? Please provide a simple C/C++ program as an example.

E.g.

main()
{
    // (10 gigabytes) / (4 bytes) = 2 684 354 560
    int *hugearray = malloc( 2684354560 * sizeof(in开发者_StackOverflowt) );
}

...obviously that doesn't work.


malloc() does allocate the memory, but most OS will only give you a virtual address space until you actually try to read or write within that memory, at which time they'll start allocating backing physical or swap memory. You simply need to loop writing some garbage values into the memory.


Sample program works fine if you change the declaration from int to long. I'm running Mint Linux on a 64-bit Intel-esque CPU with 16GB of memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号