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

Memory not released after DEALLOCATE

Alireza_B_
Beginner
1,658 Views

 I am working as a software developer in a company with a code based on FORTRAN 90. Recently we have found that code was failing with lack of enough memory at some stage of the run. Tests have shown that using the "Deallocate" makes the pointers used "unassociated", but for some pointers the memory is not released. This could be a large amount causing the code to crash eventually. The situation becomes worse as we are allocating and deallocating the same pointers several times during the runtime. For some pointer we can see that during the "allocate" memory usage increases, but it doesn't decrease by using "deallocate"!
We are using Intel 13.1 at the moment.
I will appreciate greatly if I hear your comments and advice as to how to tackle the problem?

Thanks,

Alireza

 

0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,658 Views

When you do a DEALLOCATE, the memory that was allocated returns to the pool used by the memory allocator (on Linux and OS X this is the same as C's malloc/free). The memory is not released back to the OS - it is very rare that this would even be possible. What often happens is that the pattern of allocations and deallocations causes virtual memory to be fragmented, so that while the total available space may be high, there may not be sufficient contiguous space to allocate a large item. Unlike with disks, there is no way to "defrag" memory.

If you can provide a test case that demonstrates bad behavior, we'll be glad to look at it, but the most likely explanation is what I described above.

0 Kudos
Alireza_B_
Beginner
1,658 Views

Thanks Steve. I really appreciate your comments.

In a small test program we tried, the "allocate" and "deallocate" behave as we "expect". They take the memory and release it. Only in the code we are trying we see the issue of memory not released back using the "DEALLOCATE".  This is indeed a killer as there are cases which should run for a long period with lot of allocation and deallocation and they simply fail because no memory is available for allocation only a fragmented one as you mentioned.

Do you see any way to get around the problem by managing the allocatable pointers or is that the thing we should live with?

Thanks again,

Alireza

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,658 Views

See if you can deallocate the memory in the reverse order in which it was allocated. This can reduce memory fragmentation.

Also, if you are relying on the reallocate left hand side (because it makes programming easier for you), try to reform the program such that you have control over allocations and deallocations (i.e. not use reallocate left hand side).

Try to avoid coding practices that generate temporary arrays.

As an aid in determining allocation/deallocation sequences, consider using the Fortran PreProcessor to #define ALLOCATE, and perhaps #define allocate, such that they expand a macro that emits a trace to a log file, and then performs AlLoCaTe (Fortran is case insensitive the preprocessor is case sensitive). Do the same for DEALLOCATE/deallocate. Something may show up in the sequence of how you perform allocate and deallocate.

Jim Dempsey

0 Kudos
Reply