If you want to be able to change the allocation status of an array, that array has to be allocatable or be a pointer. Without those attributes and without invoking a compiler option such as /heap-arrays, you will get the array allocated on the stack and deallocated when control leaves the subprogram of which the array is a local variable.
A different way of reducing the memory requirement is to examine your algorithm to see if you can do the processing in chunks. Does the whole 2-D array have to in memory at the same time? Could you, instead, process it a row at a time, or a column at a time, or in blocks of other shapes?
Another question is related to what you wish to do with any memory that is released. If you have no use for it in the remaining portions of the program, you can simply let the RTL and the OS do the garbage collection.
PROGRAM opinion_dynamic IMPLICIT NONE INTEGER, PARAMETER :: N= 20000 !No. of nodes INTEGER, PARAMETER :: nt= 500 !No. of time steps INTEGER, PARAMETER :: ne= 100 !No. of ensembles INTEGER, PARAMETER :: q= 20 REAL, PARAMETER :: re_prop= 0.99 !rewiring probability of w-s INTEGER, PARAMETER :: nw= 4 INTEGER :: nw1, i, j, k, u, l integer :: istat integer, allocatable :: a(:.:) REAL :: x, p OPEN (1, FILE= 'out') !HERE I need a matrix allocate( a(n,n), stat = istat) if (istat /= 0 ) stop ! allocation failed tell user /do something nw1= nw/2 a= 0 DO i= 1, n Do j=-nw1,-1 u= MOD(i+j, n) IF(u <= 0) u= u+ n a(i,u)= 1 ENDDO DO j=1,nw1 u= MOD(i+j, n) IF(u == 0) u= u+ n a(i,u)= 1 ENDDO ENDDO DO i= 1, n DO j= 1, nw1 CALL RANDOM_NUMBER (x) IF ( x > p) GOTO 125 128 CALL RANDOM_NUMBER(x) x= 1.0+ n*x l= FLOOR (x) ! new node IF (l == i) GOTO 128 !avoiding selfloop IF (a(l,i) == 1) GOTO 128 !avoiding multi link u= MOD(i+j, n) !pervious node IF(u == 0) u= u+ n a(i,u)= 0; a(u,i)= 0 a(i,l)= 1; a(l,i)= 1 125 CONTINUE !IF(sum(a) .ne. k*nw)print*,sum(a), i, u, l ENDDO ENDDO !After this, I do not need a matrix deallocate( a, stat = istat) ! clean up !other part of my code ENDPROGRAM opinion_dynamic !*******************************