1

I wrote a simple program trying to see change in memory but there was a none. A straight horizontal line around 20% comes always, whether I run the code or not.

#include<iostream>
using namespace std;
int main()
{
       int *m;
       int i;
       cin>>i;
       m = new int[i];
}

Shouldnt memory be allocated for any value of i? And a change in Memory free be shown?

user3481478
  • 387
  • 1
  • 3
  • 19
  • How are you measuring the change in memory? And you are aware that as soon as your program exits, any memory that it allocated is automatically deallocated? – Greg Hewgill Aug 07 '15 at 02:49
  • Yup I am aware of that. I wrote a while(1) infinte loop to check if memory was ever allocated but its the same effect. Using system monitor as well as the top command I am measuring the change in memory. – user3481478 Aug 07 '15 at 02:52
  • So... what value are you entering for `i`? When you say you have a baseline of 20%, that's 20% of what? Have you tried filling your allocated memory with some data, because your virtual memory system might not need to actually allocate space for your allocation unless you start writing to it? – Greg Hewgill Aug 07 '15 at 02:59
  • I have tried big to big values of i. I didn't fill my array with any values because new is anyways allocating memory. – user3481478 Aug 07 '15 at 03:02
  • Could you try running with no optimizations? And, to be sure, new an array of volatile ints. – Pradhan Aug 07 '15 at 03:20
  • @user3481478 `Using system monitor as well as the top command I am measuring the change in memory.` This is not a good way to determine what `new[ ]` is doing. The compiler's heap manager is responsible for making the ultimate calls to the system for memory, not specifically `new[ ]`. – PaulMcKenzie Aug 07 '15 at 03:37
  • 1
    Mos operating systems won't actually allocate the memory until the memory is read from or written to. If you set all the values in the array to zero you might see memory usage increase. – Jeremy Friesner Aug 07 '15 at 04:05
  • you didn't delete the array – phuclv Aug 07 '15 at 04:43

3 Answers3

3

There could be plenty of reasons why this is happening:

  1. Compiler optimization - if you turn compiler optimizations on, it could determine that your m pointer is never used, so it will simply delete new[] operator call;
  2. Operating system optimization - heap allocation is OS Memory Manager business. It could allocate memory only on the first memory usage. For example Windows API HeapAlloc method behaves that way;
  3. Your tool limitations - memory allocation/deallocation could be too fast for it to catch (basing on your example);
  4. Memory allocation failure - if your i value is too big, there is a possibility that heap manager will be unable to find continuous memory block of such size, so new operator will raise bad_alloc exception and your program will be terminated.
Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
  • I created a char array of size 6 million with new, but memory usage of the program is not increased until reading a file content into it with `ifstream::read` I understood why when I read your second argument. – atakli Nov 20 '22 at 21:24
2

A number of things might be happening...

  • compiler the compiler may be optimizing the memory allocation out (since nothing is actually done with it)
  • operating system the memory may not be allocated by the operating system since the pages aren't written to
  • sampling the resource usage may not show up on your system monitor since your program exits immediately after allocating the memory

So, to eliminate these potential causes you could write something like this...

#include <iostream>

int main(int argc, char* argv[]) {

  int x,y;

  std::cin >> x;

  int* m = new int[x];

  for (int i = 0; i < x; i++)
    m[i] = i;

  std::cin >> y;

  std::cout << m[y] << std::endl;

  return 0;
}

Then, you should be able to check memory usage before you enter the y value. I'm not sure what operating system you're using, but you should see the image size and the resident memory of the program increase roughly by 4x bytes.

Jason
  • 3,777
  • 14
  • 27
  • yes the memory is only really allocated after the first write http://stackoverflow.com/q/864416/995714 http://stackoverflow.com/q/911860/995714 http://stackoverflow.com/q/21119617/995714 – phuclv Aug 07 '15 at 04:50
  • It depends on the request size. If it's small enough, it will normally be allocated from the existing heap, which may or may not consist of pages that have memory allocated to them. If it's large enough, the heap request will generally request the memory directly from the operating system (`mmap`, `sbrk`, etc...), in which case the memory for the pages is normally lazily allocated. Another way to make sure the pages are allocated (and resident) is `mlock` or `madvise`. – Jason Aug 07 '15 at 05:00
-1

You allocated memory for maximum size of a int. That is 4 bytes. Now lets say that your RAM has 8 GB for example. 4 bytes are about 0.00000005% of 8 GB RAM.