1

I wrote a program, that allocates memory, and computes the size between adress of lowest, and highest pointer. I am suprised with results. When I start it, it takes 20mb of memory, 40, 60 ... 200, 220, 240,... 3000mb, 3020mb, and then suddenly bang, 262000 mb, and so on. Could somebody explain me why?

#include <iostream>
using namespace std;
int aaa;
int *max1=&aaa;
int *min1=&aaa;

void results(){
    cout<<"min "<<min1<<endl;
        cout<<"max "<<max1<<endl;
        double mln= 1e6;
        cout<<"min-max= "<<(max1-min1)/(1024*1024)<<"mb"<<endl;
}

void logic(int *c){
    if(c>max1){
        max1=c;
    }
    if(c<min1){
        min1=c;
    }
    static int i;
    i++;
    if(i%800==0)
    results();
}


int main(){
    int *x;
    int l=0;
    while(l<=500000000){
        l++;
        x=new int[20000];
        logic(&x[19999]);
    }

}
Ken White
  • 123,280
  • 14
  • 225
  • 444
mlecz
  • 985
  • 11
  • 19
  • Your program has a memory leak. You never called `delete[]` on the memory you used for `new[]`. – PaulMcKenzie Jan 22 '15 at 18:24
  • 2
    @PaulMcKenzie: That's obviously wanted. – Deduplicator Jan 22 '15 at 18:25
  • And the displayed message "min-max= " should be corrected into "max-min= ". – mefathy Jan 22 '15 at 18:25
  • Seems you have a big swap partition (Unixoid) or swap-file (windows). – Deduplicator Jan 22 '15 at 18:26
  • 1
    It helps a lot if you could include the capacity of your hard/solid-state disk drive. – mefathy Jan 22 '15 at 18:27
  • No, it is just how the logical address room and deferred memory allocation work on a modern OS. Access the memory and it will crash. – ypnos Jan 22 '15 at 18:28
  • 2
    You should read about [Virtual Memory](https://stackoverflow.com/questions/510544/virtual-memory) or this: https://stackoverflow.com/questions/11625729/virtual-memory – Deduplicator Jan 22 '15 at 18:28
  • As mentioned in the comments to your question, these are just logical addresses. To avoid any confusion, you can measure the amount of allocated memory by printing '(l + 1) * 20000 / 1024 / 1024' instead of subtracting addresses. This is more reliable and platform independent. – mefathy Jan 22 '15 at 18:31
  • mefathy, I do not understand @previous answers: yes memory leak is wanted. I guess my windows support max 256 GB ram, and after this amount, adresses are virtual memory – mlecz Jan 22 '15 at 18:49
  • *All* addresses are virtual. – n. m. could be an AI Jan 22 '15 at 19:46

2 Answers2

4

Comparing (and consequently subtracting) pointers that are not pointing to elements of the same array is pointless (pun intended).

You're performing pointer arithmetic on unrelated addresses, what meaning could the result have?.

You're not comparing the lowest address to the highest in the same block allocated, you're comparing the lowest address seen to the highest address seen across 800 different memory allocations.

Consider all the heap fragmentation (and possibly reclamation) that happens in-between all those calls. You could get positive, negative or zero as your difference.

If what you're trying to determine is the range of the heap, then this is too random an approach, as you have no guarantee to find the smallest and biggest addresses before you run out of memory and crash.

Also consider the effects of virtual memory and paging. With a modern implementation of virtual memory you can in theory access as much memory as your process can address (I think), regardless of how much RAM (you've actually installed.

Also, see: Allocating more memory than there exists using malloc and the link from the comments about virtual memory.

Community
  • 1
  • 1
tipaye
  • 454
  • 3
  • 6
0

Your program is allocating memory using malloc(). This gets the memory from the process heap. The heap consists of one or more contiguous ranges of pre-reserved memory. Whenever you allocate from the heap, the system will use and manipulate the heap data structures to provide you with free ranges of the desired size inside the heap and it will allocate physical memory pages for this range as needed. When you allocate a lot of memory, you will eventually consume all the space in the heap range. The system will then reserve a new range. But the new range will not neccessarily be right above the old range, since the virtual address space can be fragmented, and there is also ASLR (Address Space Layout Randomization) which causes the system to use random base addresses rather then following a consistent algorithm when reserving heap memory. So you cannot even expect consistent behaviour of your program.

Timo
  • 923
  • 9
  • 10