Hello world - what is a simple program to use 16GB of memory?
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.
精彩评论