Memory check system call on Linux


I am trying to use system calls to get the free memory on Linux at run-time. However it seems that if the virtual memory gets very low then the code will crash because it cannot launch the system call. I found this explanation which I don’t know if it is accurate:

Do you know if there is any way to avoid this problem or check free mem at run time otherwise?.
Maybe a good idea to add to fortran intrinsics efficient mem check subroutines?

thanks in advance,

1 Like

Maybe a simple solution would be instead of using a system call, just calling some external library for doing mem check without having the process “cloned” (as the link above says and hence running out of virt mem). Do you know any good library easy to use/link with for doing so to recommend?.

I feel this should be added into the intrinsics: Something like a fake allocate call that retrieves the amount of free mem.

1 Like

Hi @Seba

maybe you could try to read the info into the /proc/meminfo virtual file:

$ cat /proc/meminfo
MemTotal:        8030848 kB
MemFree:         1004692 kB
MemAvailable:    4373480 kB

I have never tried to read a virtual file from a Fortran program. But why not? It should be seen as a regular text file.

More info on the proc filesystem:

it works:

character(132) :: ligne

open (unit=1, file="/proc/meminfo", access="sequential", status="old")

    read(1, '(a)', end=200) ligne
    print *, ligne
end do

200 close(1)

$ gfortran mem.f90
$ ./a.out            
 MemTotal:        8030848 kB                                                                                                         
 MemFree:          862916 kB                                                                                                         
 MemAvailable:    4303208 kB                                                                                                         
 Buffers:          382416 kB                                                                                                         
 Cached:          3476192 kB                                                                                                         
 SwapCached:            0 kB

You now just need to analyze the interesting line (MemAvailable ?).



character(132) :: ligne
integer :: mem

open (unit=1, file="/proc/meminfo", access="sequential", status="old")

read(1, '(a)') ligne
read(1, '(a)') ligne
read(1, '(a)') ligne

print *, ligne
print *, ligne(18:24)
read(ligne(18:24), *) mem
print *, mem




$ gfortran -Wall -Wextra -std=f2018 mem.f90 && ./a.out
 MemAvailable:    4121156 kB                                                                                                         

You can use (17:24) to be sure it will work if you have more RAM.

Reading files in the /proc directory gives you a lot of informations about your machine…

Yes, I am doing something like that at run-time using “call system”/“execute command line”. Problem is that when mem is scarce it crashes due to the reason given above…

OK. But then if I just open directly the file as in your example lines (with open) then it could actually work. I’ll try it. Thanks!

Yes, with CALL EXECUTE_COMMAND_LINE, a new process is created in the memory.
Reading /proc/meminfo using Fortran instructions should be harmless.