Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C malloc : inexplicable memory usage

Tags:

c

malloc

When I compile and run the following code :(using gcc on cygwin)

int *a = malloc(1024*1024*100*sizeof(int));
while(1)
;

The task manager in Windows XP shows memory usage by this process as 2232K, which according to me should have been around 400000K.

When I compile and run the following code :(using gcc on cygwin)

int *a = malloc(1024*1024*400*sizeof(int));
while(1)
;

the memory usage goes down to 1388K;

So, rather than showing an increase, it actually shows a decline.

What could explain this?

like image 798
simplfuzz Avatar asked Feb 13 '26 16:02

simplfuzz


2 Answers

You have allocated the memory, making it available, but have not yet used it (reading or writing from/to it). The memory manager may not have actually allocated the physical memory to your program yet, merely said that you can have it. If you write something across the memory you just allocated (e.g. filling it with 0's -- look at memset for that), I would expect that the memory usage would be more in line with what you expect.

like image 164
Michael Ekstrand Avatar answered Feb 15 '26 04:02

Michael Ekstrand


Unfortunately memory consumption is not as simple as a single. There are numerous ways in whtich memory needs to be tracked (and it differs between operating systems a bit).

For instance on Windows, here are some of the different memory usage types

  • Virtual Memory
  • Physical Memory
  • Commited memory
  • Reserved Memory
  • Shared Memory

Can you give us more details on exactly which number you are talking about?

One possible explanation is that you are looking at the physical memory usage of the process. The operating system will commonly allocate virtual memory address but not commit it to physical memory until it is actually used by your process.

One way to verify this would be to set up a for loop that wrote to every element in the array and then check the memory usage of the application.

like image 42
JaredPar Avatar answered Feb 15 '26 06:02

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!