I was recently talking to somebody who said he did program Fortran(from way back), but he could not tell me if Fortran had a garbage collector. He told me he did not use malloc or free in Fortran, so my assumption is that it does have a garbage collector? Or does fortran not have a garbage collector and just leak memory, which will get reclaimed by the operating system when the program ends? I do not know anything about Fortran, except that it was used way back. I also tried a quick Google search, but could not find anything that quickly.
-
4Fortran is still used today. – Carl Norum Dec 20 '12 at 07:12
-
He told me it was still used at banks etc? use cases? – Alfred Dec 20 '12 at 07:13
-
4Scientific programming, a lot of the time. Fortran has multidimensional arrays. – Carl Norum Dec 20 '12 at 07:13
-
1http://stackoverflow.com/questions/8997039/why-is-fortran-used-for-scientific-computing – Carl Norum Dec 20 '12 at 07:16
-
1Usage of malloc and free has absolutely nothing with a garbage collector. – Vladimir F Героям слава Dec 20 '12 at 08:52
2 Answers
Modern Fortran has many ways of declaring variables. Items simply declared will exist for the duration of the scope of the entity. So "real, dimension (N) :: array" declared in a procedure will automatically disappear when that procedure returns. Naturally variables declared in the main program or module variables or common (outmoded) will persist for the duration of the program.
Variables can be dynamically allocated with "allocate" (to do so, they have to be declared with the allocatable attribute). Since Fortran 95 allocatable variables that are local to a procedure are automatically deallocated when the procedure returns! They will not leak memory! (Some programmers might consider it good practice to explicitly deallocate the variables anyway, even though it isn't strictly necessary.) (Of course, you can waste memory in the sense of not explicitly deallocating a variable that you know that you don't need anymore.)
It is possible to leak memory with pointers. You can allocate memory with a pointer, then assign the pointer to to another variable, losing the previous association. If you didn't deallocate that memory you have a leak. The need for pointers is less in Fortran than in some other languages ... many things can be done with allocatable variables, which are safer -- no memory leaks.
Related questions: Fortran allocatable array lifetime and ALLOCATABLE arrays or POINTER arrays?
-
3I will add that the same holds for allocatable derived-type components. They are automatically deallocated with the deallocation (manual or automatic) of the parent. – Vladimir F Героям слава Dec 20 '12 at 08:51
-
2Wording of your second paragraph, first sentence, excludes pointers in the parenthesised part. As an extension of "can" it is also possible in modern Fortran (2003+) to do dynamic allocation of allocatables without using an allocate statement. Third para, second sentence has meaning of assign backwards. With respect to pointers I think the standard technically *allows* for garbage collection (but explicitly doesn't *require* it) - you "may" have a leak. I thought there was actually a note or similar in recent standards about this, but I can't find it - perhaps my recollection is faulty. – IanH Dec 20 '12 at 09:06
No, Fortran does not have a garbage collector. However there is an add-on package for F90 to this extent. No, I have not used it.

- 33,938
- 5
- 80
- 91
-
It is possible to leak memory in modern Fortran, therefore a garbage collector can be used to reclaim memory. To my knowledge, the NAG Fortran Compiler is the only one offering a garbage collector in the runtime. https://www.nag.com/nagware/np/r70_doc/f90_gc.html – Themos Tsikas Feb 22 '21 at 10:06