I'm learning about OSs memory management and just learned about virtual memory and how it can be implemented using demand paging.
I made this simple program:
#include<stdio.h>
#include<stdlib.h>
int main(void){
int x=0;
scanf("%d",&x);
if(x==1){
int *a=malloc(1073741824*sizeof(int));
while(1){
for(size_t i=0;i<1073741824;i++){
a[i]=1;
}
}
}
else if(x==2){
int *b=malloc(1073741824*sizeof(int));
while(1);
}
return 0;
}
It has 2
paths:
One allocated an array of
4 gb
and keeps changing its values.The other allocated an array of
4 gb
, but doesn't change its values.
As I expected, after running the first option, the memory of the program increases to about 4 gb
, but the second one doesn't change. I'm guessing this is due to the memory not being accessed, so its pages are "swapped out" to a backing store, until they're needed again.
Is my understanding correct?