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

Getting the amount of memory used by a program

MR
Beginner
975 Views
Hi,
is there a way to know the amount of memory used by a program at
some specific location? I think that one possibility would be a system
call to "top" or analogous tools, but I wonder whether there is
already some library function provided by the compiler for this.

Thank you,
Marco Restelli

0 Kudos
5 Replies
truedas
Beginner
975 Views
At least on Unix-type systems, the command "size" should give you some information. It won't help much if you have dynamically allocated memory, however.
Thomas
0 Kudos
Ron_Green
Moderator
975 Views
I don't have the best method, but 2 things I have used in the past:

If you want to check at some point of your program, you could try this:


USE IFPORT
integer :: reslt

....when you want the memory statistics ....
reslt = system( "/bin/date >> memory_trace.txt ; /usr/bin/vmstat >> memory_trace.txt" )

and then examine the file after the run OR open the file with a normal Fortran OPEN and read the data. This is the TOTAL system statistic but in most cases your code is the only important code running on the system. You could run 'date ; vmstat' just before starting your code to get a baseline before the code starts.

If you have a server or server farm, you should consider installing GANGLIA, which records this information and more, has a nice web interface, and gives you nice charts.

We do have a feature request for a Fortran callable function to return this information at runtime. It is complicated by Linux's memory management and lazy-page philosophy. Without going down Alice's rabbit hole, when you ALLOCATE memory the OS doesn't actually do anything until you physically touch those pages (with initialization or access). On the opposite side, when you DEALLOCATE, linux may keep those pages around for you for a while, assuming that you may want to reuse them again sometime soon. So it's hard to get a handle on your high water mark like we used to be able to get in the 'good old days' before virtual memory.

and 'size myprog.exe' will do a great job of showing your static data needs, so if your code is old school F77 this may be all you need. 'size' does not capture any of your dynamically allocated data needs, however.


ron
0 Kudos
MR
Beginner
975 Views
Thomas, Ron, thank you for the indications, I will experiment with vmstat and possibly GANGLIA as well. In fact, what I want to check is almost completely dynamically allocated memory, either by explicit ALLOCATE statements or by temporaries, automatic variables, derived type copies and so on. I understand the difficulties you mention in doing this by a fortran callable function... I guess this is why we would be so happy if the compiler writers took care of it :-). Marco
0 Kudos
mriedman
Novice
975 Views
Marco, why don't you look into /proc//* for dynamic processinformation. For sure it is in there somewhere. Sorry I can't give you the exact location, some investigation is required.
I don't think vmstat will give you process specific information. michael
0 Kudos
rreis
New Contributor I
975 Views
You should try pmap. It will give you the breaking of the memory usage for your process. Try the -d flag (-x flag is also interesting)

0 Kudos
Reply