Unexpectedly high peak memory consumption of Fortran program as measured by GNU time(1)

时间:2017-04-10 01:24:31

标签: memory memory-management time fortran gnu

I am trying to measure the memory consumption of a (MPI) Fortran project that I'm working on. I measured the memory consumption with GNU's time(1) by using /usr/bin/time -v, which gives the maximum resident set size in kbytes. Because I've seen that some older versions of time had troubles with memory measurement, I wanted to make sure that it was working fine, so I wrote the following small program to test it:

program timingcheck

  implicit none
  integer :: i, s, Mb,arrlen
  integer,dimension(:), allocatable :: memarr

  i = 1
  s = sizeof(i) !number of bytes integer i occupies
  Mb = 1024*1024

  !Allocate array of size 100 Mb
  arrlen = 100 * Mb / s
  allocate(memarr(1:arrlen)) 

  memarr = 1

  call sleep(3)

  deallocate(memarr)

end program timingcheck

So far, so good. I compile the program with gfortran, without any special flags. By running /usr/bin/time -v ./timingcheck.out I get:

Command being timed: "./timingcheck.out"
User time (seconds): 0.02
System time (seconds): 0.00
Percent of CPU this job got: 1%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.03
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 104144
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 655
Voluntary context switches: 2
Involuntary context switches: 3
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0

So /usr/bin/time gives me the maximal memory consumption as 104144 kb, which translates to 101.7 Mb.

Here's the question: where do those 1.7 Mb extra memory usage come from? I see multiple possibilities:

  • I made a mistake in my program that I'm not aware of
  • There is some extra memory needed in Fortran's way of storing the array that I'm not aware of
  • it's because of an imprecision of /usr/bin/time's memory measurement

Is it one (or multiple) of those? Or something completely different?

Edit: ldd timingcheck.out gives:

linux-vdso.so.1 =>  (0x00007ffde1b53000)
libgfortran.so.3 => /usr/lib/x86_64-linux-gnu/libgfortran.so.3 (0x00007f421485d000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4214494000)
libquadmath.so.0 => /usr/lib/x86_64-linux-gnu/libquadmath.so.0 (0x00007f4214254000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4213f4b000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4213d35000)
/lib64/ld-linux-x86-64.so.2 (0x000055c3d15ec000)

Edit 2: I've run the measurement for different array sizes. These are the results:

Intended array size:    Measured Peak Memory Usage:
10  Mb                  12.07  Mb
50  Mb                  51.98  Mb
100 Mb                  101.83 Mb   
200 Mb                  201.87 Mb       
500 Mb                  502.03 Mb

Interestingly enough, the peak memory consumption seems to vary a little each time I run the measurement. What might be the reason behind this?

0 个答案:

没有答案