I have the following question:how do I deallocate array memory in type? Like a%b%c, how do I deallocate c? the specific problem is(The compiler environment I tried are gfortran version gcc4.4.7 and ifort version 18.0.1.):
[Fortran] 纯文本查看 复制代码 module grist_domain_types
implicit none
public :: aaa
type bbb
real (8), allocatable :: c(:)
end type bbb
type aaa
type(bbb), allocatable :: b(:)
end type aaa
end module grist_domain_types
program main
use grist_domain_types
type(aaa) :: a
integer(4) :: time,i
time=20
allocate(a%b(1:100000000))
call sleep(time)!--------------1
do i=1,100000000
allocate(a%b(i)%c(1:1))
enddo
call sleep(time)!--------------2
do i=1,100000000
deallocate(a%b(i)%c)
enddo
call sleep(time)!--------------3
deallocate(a%b)
call sleep(time)!--------------4
end program
First,I run this program. Then I use top -p processID to see memory. When the program is executed to 1, the memory is 4.5G. When the program is executed to 2, the memory is 7.5G. When the program is executed to 3, the memory is also 7.5G(but I think is 4.5G). When the program is executed to 4, the memory is 3G(I think is 0G or close to 0G). So deallocate(a%b(i)%c) does not seem to work. However, I use valgrind to see memory. the memory of this program is all deallocate...I used ifort and gfortran. This problem happens no matter which compiler I use. How to explain this question? I allocate many c array in this way,the program will finally crash due to insufficient memory. And how to solve it? 简而言之就是我用dellocate释放a%b(i)%c中的c数组时,内存不会减少,不会被释放掉。我原始程序中会分配很多这样的c数组,到最后程序就崩溃了。原因可能是应该释放掉了(我用valgrind工具看过),但是没有告知操作系统。操作系统不知道。在这里问问,看看有没有遇到同样问题的大佬,怎么释放c数组的内存并且告知操作系统不让我的程序崩溃?求大神帮忙!!!可以复制我上面的测试代码看看。测试步骤:编译上面的代码,然后运行,使用top -p + 进程号查看内存,或者其它方式,程序运行到1处,内存消耗4.5G,2处,7.5G,运行到3处按道理c数组释放了应该是4.5G,但是系统显示还是7.5G,运行到4处应该全部释放掉了应该是0G或者接近0G,但是显示的是3G。
|