Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Memory leak: a minimal example

mchoi
Novice
667 Views

Hello, 

 

I have found that a constantly deallocated memory at the end of the subroutine (or even the program). I am wondering if I made anything wrong here. I suppose that all allocated memories must be deallocated at the end. But the diagnostic tool tells me 'Allocations (diff)' is still +5 at the end. The example code is attached.

 

Thank you.

 

test_leak.png

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
606 Views

I am not seeing a problem here, and your description seems strange.

The memory you allocate with ALLOCATE is completely deallocated by the DEALLOCATE. There is additional memory that has been allocated for the I/O operations - those are not explicitly deallocated by the program but go away when the process exits.

In the screenshot below, I did a snapshot before and after the ALLOCATE and before and after the DEALLOCATE.

Screenshot 2026-04-05 182819.png
 
 
 
 
 
I then removed the PRINT and saw this:
 
Screenshot 2026-04-05 183050.png
 
 
 
 
 
Looks perfectly normal to me.

View solution in original post

4 Replies
jdelia1
Novice
582 Views

Hi,

Try: (i) containing the subroutine within the main program, (ii) using a module. For example, in the first case,

program leak_test
    implicit none
    double precision, allocatable :: mat(:,:)
    integer :: i
    allocate (mat(100, 100))
    do i = 1, 100000

        call sub_calc(mat)
        if (mod(i, 1000) == 0) print *, "iteration: ", i
    end do
    deallocate(mat)
    contains
    subroutine sub_calc(a)
        implicit none
        double precision :: a(1000, 1000)
        a(1,1) = a(1,1) + 1.0d0
    end subroutine sub_calc
end program leak_test

 

Then,

 

$ ifx --version
ifx (IFX) 2025.3.3 20260319
Copyright (C) 1985-2026 Intel Corporation. All rights reserved.

$ ifx -mtune=generic -std18 -WB -warn all -check all -o testcase-0222-ifx.exe testcase-0222.f90

$ testcase-0222-ifx.exe

iteration: 1000
iteration: 2000
...
iteration: 98000
iteration: 99000
iteration: 100000

 

Regards,

Jorge D'Elia

 

 


@mchoi wrote:

Hello, 

 

I have found that a constantly deallocated memory at the end of the subroutine (or even the program). I am wondering if I made anything wrong here. I suppose that all allocated memories must be deallocated at the end. But the diagnostic tool tells me 'Allocations (diff)' is still +5 at the end. The example code is attached.

 

Thank you.

 

test_leak.png


 

jdelia1
Novice
582 Views

Hi,

Try: (i) using a module; (ii) containing the subroutine within the main program. For example, in the second case:

program leak_test
    implicit none
    double precision, allocatable :: mat(:,:)
    integer :: i 
    allocate (mat(100, 100))
    do i = 1, 100000
    call sub_calc(mat)
    if (mod(i, 1000) == 0) print *, "iteration: ", i
    end do
    deallocate(mat)
contains
    subroutine sub_calc(a)
        implicit none
        double precision :: a(1000, 1000)
        a(1,1) = a(1,1) + 1.0d0
    end subroutine sub_calc
end program leak_test

$ ifx --version
ifx (IFX) 2025.3.3 20260319
Copyright (C) 1985-2026 Intel Corporation. All rights reserved.

$ ifx -mtune=generic -std18 -WB -warn all -check all -o testcase-0222-ifx.exe testcase-0222.f90
$ testcase-0222-ifx.exe
iteration: 1000
iteration: 2000

...

iteration: 99000
iteration: 100000

 

Regards.

Jorge.

Steve_Lionel
Honored Contributor III
607 Views

I am not seeing a problem here, and your description seems strange.

The memory you allocate with ALLOCATE is completely deallocated by the DEALLOCATE. There is additional memory that has been allocated for the I/O operations - those are not explicitly deallocated by the program but go away when the process exits.

In the screenshot below, I did a snapshot before and after the ALLOCATE and before and after the DEALLOCATE.

Screenshot 2026-04-05 182819.png
 
 
 
 
 
I then removed the PRINT and saw this:
 
Screenshot 2026-04-05 183050.png
 
 
 
 
 
Looks perfectly normal to me.
mchoi
Novice
569 Views

Thank you. Yes, here, after removing the PRINT, I have no more additional memory allocation. It is now +0 in total.

test_leak_revised.png

 

0 Kudos
Reply