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

How to check avaliable memory in fortran

haijunwu
Beginner
1,884 Views
Hi, I want to check the avaliable memory of my pc before I allocate a huge size array.

Is there any bulid in function or subroutine avaliable in fortran or other methods to query thememory?

thanks,
0 Kudos
10 Replies
Arjen_Markus
Honored Contributor I
1,884 Views
There is nothing standard, but what you can do with the allocate statement
is catch any error:

allocate( array(10**8), stat = ierr )
if ( ierr /= 0 ) then
write(*,*) 'Oops, allocation failed!'
endif

Regards,

Arjen
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,884 Views
There are two problems (well there are more than two):

1) Does the allocation have available Virtual Memory addresses.
2) Does the system have available physical memory.

When an allocation runs out of physical memory, but has Virtual Memory addresses available, the allocation will succeed (provided paging file capacity is not limited and has sufficient disk space for expansion). An application programmer may wish to not exceed the physical memory capacity as this would impact performance.

Other than for allocation failure, Fortran does not have a built-in function for making this determination (near exceeding physical memory capacity, and/or near exceeding page file capacity, and/or near exceeding virtual memory capacity). However, the allocations in Fortran usually interface to the C Runtime Library and the Operating System Heap Manager Library. These libraries usually do have query functions to determine capacities of these natures. Start with searching MSDN for heap management functions (same for Linux if you want portability).

Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
1,884 Views
There is nothing available from C that you can't do from Fortran. The problem is that there is no way to know if an allocation will succeed until you try it, due to the nature of virtual memory. There is only the theoretical limit of the possible virtual address space.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,884 Views
Steve,

Thereis more to itthan if an allocation will succeed or fail. You (the programmer) may also be interested in ifthe allocation will place the application into a paging state. (iow when physical memory is occupied while page file and virtual memory as room for additional allocations).

For example, one might want to use:
void GlobalMemoryStatus(
  LPMEMORYSTATUS lpBuffer
);



The MEMORYSTATUS structure contains information about the current state of both physical and virtual memory. The GlobalMemoryStatus function stores information in a MEMORYSTATUS structure.

typedef struct _MEMORYSTATUS {
DWORD dwLength;
DWORD dwMemoryLoad;
SIZE_T dwTotalPhys;
SIZE_T dwAvailPhys;
SIZE_T dwTotalPageFile;
SIZE_T dwAvailPageFile;
SIZE_T dwTotalVirtual;
SIZE_T dwAvailVirtual; } MEMORYSTATUS,
*LPMEMORYSTATUS;

Members

dwLength
Size of the MEMORYSTATUS data structure, in bytes. You do not need to set this member before calling the GlobalMemoryStatus function; the function sets it.
dwMemoryLoad
Number between 0 and 100 that specifies the approximate percentage of physical memory that is in use (0 indicates no memory use and 100 indicates full memory use).
WindowsNT:Percentage of approximately the last 1000 pages of physical memory that is in use.
dwTotalPhys
Total size of physical memory, in bytes.
dwAvailPhys
Size of physical memory available, in bytes.
dwTotalPageFile
Size of the committed memory limit, in bytes.
dwAvailPageFile
Size of available memory to commit, in bytes.
dwTotalVirtual
Total size of the user mode portion of the virtual address space of the calling process, in bytes.
dwAvailVirtual
Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.

Remarks

MEMORYSTATUS reflects the state of memory at the time of the call. It reflects the size of the paging file at that time. The operating system can enlarge the paging file up to the maximum size set by the administrator.

On computers with more than 4 GB of memory, the MEMORYSTATUS structure can return incorrect information. Windows reports a value of -1 to indicate an overflow, while Windows NT reports a value that is the real amount of memory, modulo 4 GB. If your application is at risk for this behavior, use the GlobalMemoryStatusEx function instead of the GlobalMemoryStatus function.


BOOL GlobalMemoryStatusEx(
  LPMEMORYSTATUSEX lpBuffer
);
typedef struct _MEMORYSTATUSEX {
DWORD dwLength;
DWORD dwMemoryLoad;
DWORDLONG ullTotalPhys;
DWORDLONG ullAvailPhys;
DWORDLONG ullTotalPageFile;
DWORDLONG ullAvailPageFile;
DWORDLONG ullTotalVirtual;
DWORDLONG ullAvailVirtual;
DWORDLONG ullAvailExtendedVirtual; } MEMORYSTATUSEX,
*LPMEMORYSTATUSEX;

Members

dwLength
Size of the structure, in bytes. You must set this member before calling GlobalMemoryStatusEx.
dwMemoryLoad
Number between 0 and 100 that specifies the approximate percentage of physical memory that is in use (0 indicates no memory use and 100 indicates full memory use).
ullTotalPhys
Total size of physical memory, in bytes.
ullAvailPhys
Size of physical memory available, in bytes.
ullTotalPageFile
Size of the committed memory limit, in bytes. This is physical memory plus the size of the page file, minus a small overhead.
ullAvailPageFile
Size of available memory to commit, in bytes. The limit is ullTotalPageFile.
ullTotalVirtual
Total size of the user mode portion of the virtual address space of the calling process, in bytes.
ullAvailVirtual
Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.
ullAvailExtendedVirtual
Size of unreserved and uncommitted memory in the extended portion of the virtual address space of the calling process, in bytes.

Remarks

MEMORYSTATUSEX reflects the state of memory at the time of the call. It reflects the size of the paging file at that time. The operating system can enlarge the paging file up to the maximum size set by the administrator.


Jim Dempsey






0 Kudos
Steven_L_Intel1
Employee
1,884 Views
Jim,

Perhaps you are not aware that we provide a sample of calling the MemoryStatus routines. In a sample, unsurprisingly, called MemoryStatus in the Win32 collection. I have not found this to be very helpful in predicting how much memory one can allocate, though it certainly can put an upper limit on it.
0 Kudos
Paul_Curtis
Valued Contributor I
1,884 Views
One of the main limitations of the Global Memory Status API calls is precisely that the memory in question is global, and is constantly being allocated/deallocated by other active processes including many many background Windows activities, so this information turns out to be pretty much worthless for assessing exact memory availability or use (as in trying to track a memory leak) by one's own fortran program.
0 Kudos
SergeyKostrov
Valued Contributor II
1,884 Views
>>...Huge sizearray...

Could you be more specific, please?

Also, I expect that you're using a Windows OS and if Yesyou can do a very simple thing:

In System Properties set anInitial Size of Virtual Memory to 2,048 MB and Maximum Size to 4,096 MB. You could be asked to reboot your computer.

Remember, that on Win32 platforms your application will be able to allocate up to 2 GB of memory.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,884 Views
Sketch code:

bool SuggestYouStopAllocating()
{
MEMORYSTATUSEX ms;
if(!GlobalMemoryStatusEx(&ms))
return false; // ?? function call error ??
return(ms.ullTotalVirtual > (ms.ullTotalPhys / 2)); // stop at 1/2 load
// return(ms.ullTotalVirtual > ((ms.ullTotalPhys / 2)+(ms.ullTotalPhys / 4))); // stop at3/4 load
}

Adjust the threshold condition. The above uses 50% or 75%

Jim Dempsey
0 Kudos
SergeyKostrov
Valued Contributor II
1,884 Views
Note: 2nd post as Non-Private. Sorry about this.

>>...
>>bool SuggestYouStopAllocating()
>>...

It's an impressive name! :)

I remember even amore impressivenaming in a C source fileone of the firstWindows SDK from Microsoft( it was still for Win16! ). It looked somethinglike this:

< some function >
{
...
if( ... )
goto CantBelieveThatIUsedGoTo;
...
CantBelieveThatIUsedGoTo:
...
}

Best regards,
Sergey
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,884 Views
It is generally a good programming practice to use a function name that is self explanitory. Then you do not have to comment it or have the problem of having a typographical error call an unintended function. Back in my earlier programming days when symbols could only have 1 letter (BASIC) or 6 letters (assembler), it was difficult to have a large program with meaningful symbols. Think of vanity license plates and you get an idea of how inventive you had to be.

BTW The GOTO is not dead. Use sparingly, and only when needed.

Jim Dempsey
0 Kudos
Reply