Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does fortran have a garbage collector(gc)?

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.

like image 873
Alfred Avatar asked Dec 20 '12 07:12

Alfred


People also ask

Does Fortran have garbage collector?

No, Fortran does not have a garbage collector.

What are garbage collected languages?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.

Does C++ has a garbage collector?

Existing C++ standard of 1998/2004 does not specify a garbage collector. The upcoming standard C++0x does specify an optional garbage collector API, however the implementation is an other part. With all that said, there are garbage collectors available for C++ from compiler vendors and third party.

Is Java a garbage collected language?

Java garbage collection is an automatic process. The programmer does not need to explicitly mark objects to be deleted. The garbage collection implementation lives in the JVM. Each JVM can implement garbage collection however it pleases; the only requirement is that it meets the JVM specification.


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?

like image 186
M. S. B. Avatar answered Oct 01 '22 05:10

M. S. B.


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.

like image 20
hd1 Avatar answered Oct 01 '22 05:10

hd1