Track memory usage in Fortran 90

fortran, memory

Solution

liskawc has a very nice solution and I've been looking for something like this for a while.

He asked for feedback, and there were a couple of areas that could be improved.

- there are several system calls that can be eliminated by just reading the system file directly from your Fortran program

- the solution depends on a temporary file in the users directory

- my fortran compiler didn't like opening a file starting with the tilde

I've modified his original program to overcome these issues:

subroutine system_mem_usage(valueRSS)
implicit none
use ifport !if on intel compiler
integer, intent(out) :: valueRSS

character(len=200):: filename=' '
character(len=80) :: line
character(len=8)  :: pid_char=' '
integer :: pid
logical :: ifxst

valueRSS=-1    ! return negative number if not found

!--- get process ID

pid=getpid()
write(pid_char,'(I8)') pid
filename='/proc/'//trim(adjustl(pid_char))//'/status'

!--- read system file

inquire (file=filename,exist=ifxst)
if (.not.ifxst) then
  write (*,*) 'system file does not exist'
  return
endif

open(unit=100, file=filename, action='read')
do
  read (100,'(a)',end=120) line
  if (line(1:6).eq.'VmRSS:') then
     read (line(7:),*) valueRSS
     exit
  endif
enddo
120 continue
close(100)

return
end subroutine system_mem_usage

Please feel free to update if you can improve this program any further!

Problem

I am trying to track the memory usage and cpu time of a subroutine in a Fortran 90 program. To track the track the cpu time, I use the following: `call cpu_time(tic) call subroutine(args) call cpu_time(toc) time = toc-tic` Is there a way to do something similar to record memory usage? What is the best way to do this? Thanks in advance for the help.

Original source