How to release memory?

This may sound silly, but I'm a little confused right now. Why won't this program consume all the memory? For example: I have the following program running on a Linux terminal (2G RAM),

#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 that the int size in this machine is 4 bytes, then for 2 GB of memory, it can only hold 2 ^ 30/2 ^ 2 = 2 ^ 28

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

Added: I just want to make sure that I understand it correctly. If there is no virtual memory or os optimization, etc., 2GBRAM can only contain 2 ^ 28 int, right? In this case, will the above program consume all the memory? You know how I can disable virtual memory / swap functions, etc. In Linux?

Thanks!

+4
source share
5 answers

Your calculations seem suspicious.

2 20 = 1,048,576
2 11 = 2.048

To highlight a billion integers, try 2 30 . Note that your vector<int*> will take up a considerable amount of space (at least as much as the integers you allocate). Just to run out of memory, you don't need it at all. You can call new int(i) and throw away the returned pointer, and memory will still be allocated.

Remember also that your machine most likely has virtual memory outside the installed physical capacity of 2 GB.

+4
source

Due to virtual addressing, you can allocate more memory than it actually is in terms of RAM. The OS will automatically exit the memory (on the hard drive) that you are not using. Thus, your RAM serves as a large cache for the swap file on your hard drive, which represents the actual memory of your system.

Your actual limit is the pointer address space, which, if you do not compile (and do not execute), the 64-bit platform is 32-bit. Thus, you can allocate 4 GB of space.

+7
source

Your program does nothing useful in its memory. Therefore, the optimizer can optimize it for:

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

2 GB is the same as two billion bytes. This means that your RAM may contain about 500 million pointers. Now your system also has a swap file for storage, which is probably as large as your RAM or larger.

2 ^ 20 - about a million. Therefore, while your program eats part of your memory, it does not use all of this. Just increase this pow () call to a ridiculously large number and it will work. Enjoy!

Also: there is no guarantee that sizeof (int) == sizeof (int *), although this is true for many systems.

0
source

There are some misunderstandings in the use of memory.

  • 2 GB of memory does not mean that you can use only 2 GB of memory. (1) We have a swap space; (2) Linux overrides.

  • new int does not accept 4 bytes. Memory overhead

  • You cannot use all memory. Fragmentation of memory, section .text, etc. All take up memory space.

0
source

All Articles