2
mac osx (catalina)
gfortran 9.3.0 from homebrew
htop 2.2.0 from homebrew

I have the following program in memtest.f90 which I compile with gfortran memtest.f90 -o test and then call with ./test

program main
  implicit none
  integer, parameter :: n=100000000
  real, allocatable :: values(:)
  print *, "no memory used yet, press enter"
  read(*,*)
  allocate(values(n))
  values = 0.0
  print *, "used a lot of memory, press enter"
  read(*,*)
  deallocate(values)
  print *, "why is the memory still there in htop"
  read(*,*)
end program main

I am expecting the memory used by the program to drop after calling the deallocate statement, however, as indicated by htop it continues to hover at about 382 MB (see image below)

is this a memory leak and if so how do I properly release the memory or am I just doing something wrong in looking at the memory consumed by the program?

enter image description here

Vince W.
  • 3,561
  • 3
  • 31
  • 59

1 Answers1

4

The program will typically not return the memory to the operating system below some threshold. It may also take some time to be freed. This is not a Fortran issue, but rather a system issue.

I did not mark it as a duplicate of this Will malloc implementations return free-ed memory back to the system? because it is quite indirect, and deserves some commenting, but the issue is there. Fortran compilers typically call the malloc provided by the operating system's or accompanying C-compiler's C library.

  • 1
    Fascinating. I added an extra zero to N so that the program consumed ~4GB rather than ~400 MB and in that case it did return the memory to the system. Thanks much – Vince W. Jun 16 '20 at 21:39