首页 > 解决方案 > htop 显示内存仍在使用,即使调用了 deallocate

问题描述

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

我有以下程序memtest.f90,我在其中编译gfortran memtest.f90 -o test然后调用./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

我预计程序使用的内存在调用 deallocate 语句后会下降,但是,正如它所表明的那样,htop它继续徘徊在大约 382 MB(见下图)

这是内存泄漏吗?如果是,我该如何正确释放内存,或者我只是在查看程序消耗的内存时做错了什么?

在此处输入图像描述

标签: memoryfortran

解决方案


该程序通常不会将内存返回到低于某个阈值的操作系统。也可能需要一些时间才能被释放。这不是 Fortran 问题,而是系统问题。

我没有将它标记为这个的副本malloc 实现会将释放的内存返回给系统吗?因为它是相当间接的,值得一些评论,但问题就在那里。Fortran 编译器通常调用malloc操作系统提供的或随附的 C 编译器的 C 库。


推荐阅读