This is more of a best practice on Fortran code writing other than solving an error.
I have this following code sample with some large array that needs to be passed around to some subroutine for some calculation
program name
    implicit none
    integer, parameter:: n = 10**8
    complex(kind=8) :: x(n)
    integer :: i, nVal 
    nVal = 30
    do i =1,1000
        call test(x,nVal)
        !-----other calculations-----!
        ! after every step nVal chnages, and after few step nVal converges
        ! e.g. `nVal` starts from 30 and converges at 14, after 10-15 steps, and stays there for rest of the loop
        ! once `nVal` converges the `workarray` requires much less memory than it requires at the starts
    enddo
    contains
    subroutine test(arr,m)
        integer , intent(inout) :: m 
        complex(kind=8), intent(inout) :: arr(n)
        complex(kind=8) :: workarray(n,m) ! <-- large workspace
        !----- do calculation-----------!
        !--- check convergence of `m`----! 
    end
end program name
The internal workarray depends on a value that decreases gradually and reaches a convergence, and stays there for rest of the code. If I check the memory usage with top it shows at 27% from starts to finish. But after few steps the memory requirement should decrease too.
So, I modified the code to use allocatable workarray like this,
program name
    implicit none
    integer, parameter:: n = 10**8
    complex(kind=8) :: x(n)
    integer :: i, nVal, oldVal
    complex(kind=8), allocatable :: workarray(:,:)
    nVal = 30 
    oldVal = nVal
    allocate(workarray(n,nVal))
    do i =1,1000
        ! all calculation of the subroutine `test` brought to this main code
        !--- check convergence of `nVal`----! 
        if(nVal /= oldVal) then
            deallocate(workarray)
            allocate(workarray(n,nVal))
            oldVal = nVal
        endif
    enddo
end program name
Now, If I use top the memory usage starts at about 28% and then decreases and reaches a converged value of 19%.
Now, my question is how should I code situations like this. The allocatable option do decreases memory requirement but it also hampers the code readability a little bit and introduces code duplication in several places. On the other hand, the prior option keeps larger memory for the whole time where much less memory would suffice. So, what is preferred way of coding in this situation?
 
    