MAPL_MemUtilsFree Subroutine

public subroutine MAPL_MemUtilsFree(totmemfree, RC)

Arguments

Type IntentOptional Attributes Name
real, intent(out) :: totmemfree
integer, intent(out), optional :: RC

Calls

proc~~mapl_memutilsfree~~CallsGraph proc~mapl_memutilsfree MAPL_MemUtilsFree proc~mapl_return MAPL_Return proc~mapl_memutilsfree->proc~mapl_return proc~mapl_verify MAPL_Verify proc~mapl_memutilsfree->proc~mapl_verify at at proc~mapl_return->at insert insert proc~mapl_return->insert proc~mapl_throw_exception MAPL_throw_exception proc~mapl_return->proc~mapl_throw_exception proc~mapl_verify->proc~mapl_throw_exception

Source Code

subroutine MAPL_MemUtilsFree ( totmemfree, RC )

real, intent(out) :: totmemfree
integer, optional, intent(OUT  ) :: RC

! This routine returns the amount of free memory on Linux systems.
! It does this by querying a system file (file_name below).

character(len=32) :: meminfo   = '/proc/meminfo'
character(len=32) :: string
real    :: memfree, buffers, cached
integer :: mem_unit
real    :: multiplier

character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtilsFree"
integer :: status

  totmemfree = 0.0
  memfree = 0.0
  buffers = 0.0
  cached = 0.0
  multiplier = 1.0

  call get_unit(mem_unit)
  open(UNIT=mem_unit,FILE=meminfo,FORM='formatted',IOSTAT=STATUS)
  _VERIFY(STATUS)
  do; read (mem_unit,'(a)', end=20) string
    if ( INDEX ( string, 'MemFree:' ) == 1 ) then  ! Free memory
      read (string(9:LEN_TRIM(string)-2),*) memfree
      if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
        multiplier = 1.0/1024. ! Convert from kB to MB
      memfree = memfree * multiplier
    endif
    if ( INDEX ( string, 'Buffers:' ) == 1 ) then  ! Buffers
      read (string(9:LEN_TRIM(string)-2),*) buffers
      if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
        multiplier = 1.0/1024. ! Convert from kB to MB
      buffers = buffers * multiplier
    endif
    if ( INDEX ( string, 'Cached:' ) == 1 ) then  ! Cached
      read (string(8:LEN_TRIM(string)-2),*) cached
      if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
        multiplier = 1.0/1024. ! Convert from kB to MB
      cached = cached * multiplier
    endif
  enddo
20 close(mem_unit)

  totmemfree = memfree + cached + buffers

   _RETURN(ESMF_SUCCESS)
end subroutine MAPL_MemUtilsFree