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

Garbage collection

dbruceg
Beginner
1,698 Views

Are there any general rules one should follow for memory allocation/deallocation to improve the efficiency of garbage collection? For example,

allocate (a(50),stat=...

allocate (b(50),stat=...

deallocate (b)

deallocate (a)

presumably works like a stack. But does it buy you anything?

Bruce Gerdes

0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,698 Views
There is no "garbage collection", which would imply a way to detect which allocated chunks of memory were no longer pointed to by a valid pointer. In Fortran that's not possible. Note that allocatable variables will automatically be deallocated when no longer in scope.

There's no good reason to deallocate things in a particular order.
0 Kudos
dbruceg
Beginner
1,698 Views

Thanks, Steve. "Garbage collection" was a poor description of the phenomenon I'm considering. A better term would have been "heap fragmentation.".

While I've found it practically impossible to completely avoid heap fragmentation, I've always been able to find or create waypoints in the execution at which I can defrag the heap using I/O. Between those points, however, I often find myself fragging the memory with rubber arrays. If array needs to grow, I allocate a temporary pointer for an array with, say, double the size of the original; move the original array into the new one; deallocate the original; redirect the original pointer to the new array; and nullify the temporary pointer. Quite obviously, that leaves a hole where the original array used to be.

Is there aFortran "heap manager" which recognizes the existence of this hole? It seems that there must be somesuch form of monitoring, or Fortran would apparently be incapable of reusing deallocated memory. It may not actuallyuse the hole for new allocations, but it seems that if one deallocates all the memoryabove the hole,Fortran must somehow recognizethat the top of the heap is now at thebottom of the hole.

Operating on the hypothesis that Fortran keeps some sort of heap table,it seems that the most efficientdeallocation sequence would be from the top of the heap down. To do otherwise would create additional holes and therefore additional headaches for the heap manager.

Does this make any sense? Am I overcomplicating a much simpler operation?

Bruce

0 Kudos
dbruceg
Beginner
1,698 Views

Upon further reflection, the issue may have a very short answer. That answer might be, "IFort uses malloc and free," or, "IFort uses the Windows memory management functions."

0 Kudos
Steven_L_Intel1
Employee
1,698 Views
Fortran does not have its own heap manager. It uses either the C library's malloc or the Win32 routine VirtualAlloc, depending on the allocation size. It relies on those other libraries to manage the heap. I assume that colalescing of adjacent free blocks is done automatically.
0 Kudos
dbruceg
Beginner
1,698 Views
thxdbg.
0 Kudos
Reply