Is memory allocated actually in use?

In an earlier article, we talked about actually initializing memory rather than just allocating it. This post will show how we can look at what is allocated in Linux, as well as when it shows as being in use and when it doesn’t.

I found that memory will show up in pmap (really, pmap just reads /proc/pid/maps and formats it) if it has been malloc’d.

It will *not* show up in free-m or /proc/meminfo unless it has been initialized/written to, i.e., calloc() or memset().

The JRE must malloc() 1.2GB of RAM per process, but not call memset(). Once again, in this case the memory will not show up as used in free –m if this is the case (as it is with us). This is also what Oracle does with the SGA, which is why we don’t see free memory reduced by 8GB immediately after we start an instance, but only over time.

Using the C code below…

int main() {
  void *m = malloc(2000*1024*1024);
  memset(m,0,2000*1024*1024);
  sleep(30);
  return 0;
}

…we start with the server showing 0MB swap used, 5500MB of file buffers/cache, and 700MB free memory…

21:05:31 oracle@emgrid01 ~ >free -m
             total       used       free     shared    buffers     cached
Mem:          7987       7287        700          0        624       4902
-/+ buffers/cache:       1761       6226
Swap:         1004          0       1004

…I then malloc()/memset() the 2GB of memory…

21:05:33 oracle@emgrid01 ~ >./allocatemem &
[1] 10616

…after which I show 2GB of memory as writable-private in my process…

21:05:39 oracle@emgrid01 ~ >pmap -d 10616 | tail -1
2048140K writable-private, 10580K readonly-private, and 0K shared

…so the server now shows we have 650MB less free memory, 1400MB less file buffer/cache used, and an increase of 80MB of swap used (why was the swap used rather than pulling from file cache??)…

21:05:46 oracle@emgrid01 ~ >free -m
             total       used       free     shared    buffers     cached
Mem:          7987       7940         47          0        302       3798
-/+ buffers/cache:       3839       4148
Swap:         1004         80        923
21:05:49 oracle@emgrid01 ~ >

…and when the process terminates, the OS dutifully cleans up behind me and we see the swap is still there, and the file buffers/cache that were in use at the start of our test have now been returned to the free pool…

[1]+  Done                    ./allocatemem
21:07:00 oracle@emgrid01 ~ >free -m
             total       used       free     shared    buffers     cached
Mem:          7987       5935       2052          0        301       3798
-/+ buffers/cache:       1835       6152
Swap:         1004         80        923
21:07:03 oracle@emgrid01 ~ >

If I run the test with the memset() call commented out, pmap –d will still show the 2GB as writable-private in my process, but the free server memory as shown by free –m will not be reduced, as it has only been allocated.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.