开发者

how to exhaust memory?

It might look silly, but I'm kind of confused about this now. Why this program won't consume all the memory? For example: I have the following program running in a Linux(2G RAM) terminal,

  #include <iostream>
  #include <cmath>
  using namespace std;

  int main()
  {
     cout<<sizeof(int*)<<endl;
     for(int i=0; i<pow(2.0,30.0);i++)
       {
         new int(i);
       }
    return 1;
  }

1) I confirmed the int size in this machine is 4 bytes, then for 2GB ram, it can only hold 2^30/2^2=2^28

2) following the logic above, how could you change the program actually consume all the 2GB memory?

Added: I just want to make sure I understand it correctl开发者_C百科y theoreticaly. If there is no virtual memory or os optimization etc.., 2GBRAM only could hold 2^28 int, is that right? In that case, the above program will consume all memory? Do you know how I could turn off the virtual memory/swap memory features etc.. in a linux?

Thanks!


Because of virtual addressing, you can allocate more memory than you actually have in terms of RAM. The OS will automatically page out memory (to the hard drive) that you're not using. In this way, your RAM serves as a large cache to the hard disk's swap file, which represents the actual memory of your system.

Your actual limit is the address space of a pointer, which unless you're compiling for (and running on) a 64-bit platform is 32-bits. So you can allocate 4GB of space.


Your calculations seem suspect.

220 = 1,048,576
211 = 2,048

To allocate a billion integers, try 230. Note that your vector<int*> will take a significant amount of space (at least as much as the integers you're allocating). To simply exhaust memory, you don't need that at all. You can call new int(i) and throw away the returned pointer, and the memory will still be allocated.

Remember also that your machine likely has virtual memory beyond the 2 GB physical RAM you have installed.


Your program doesn't do anything useful with its memory. The optimizer can therefore optimize it to:

  #include <iostream>

  int main()
  {
     std::cout<<sizeof(int*)<<endl;
     for(int i=0; i<(1<<10);i++)
       {
         // nothing
       }
    return 1;
  }


2 GB is the same as two billion bytes. This means that your RAM can hold approximately 500 million int pointers. Now, your system also has a swap file for storage, which is probably as big as your RAM or bigger.

2 ^ 20 is about one million. So while your program eats some of your memory, it does not use it all up. Just increase that pow() call to a ridiculously large number, and it will work. Have fun!

Also: There is no guarantee that sizeof(int) == sizeof(int*), although it's true for many systems.


There are some misconception on memory usage.

  1. 2GB memory machine don't means you can only use 2GB memory. (1) We have swap space ; (2) Linux overcommits.

  2. new int does not take 4 byte. There are memory overhead

  3. You can't use all the memory. Memory fragmentation, .text section, etc.. all take up memory space.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜