allocate():
program mem_test
implicit none
integer, allocatable :: a(:,:,:)
integer ierr, n1, n2, L, method
n1 = 250000 ; n2 = 1000 !! 1-GB subarray
print *, "Input: method, L"
read *, method, L
select case ( method )
case ( 1 )
allocate( a( n1, n2, L ) ) !! request L-GB virtual mem
case ( 2 )
allocate( a( n1, n2, L ), stat=ierr ) !! request L-GB virtual mem
if ( ierr /= 0 ) stop "Memory error!"
case ( 3 )
allocate( a( n1, n2, L ), source=0 ) !! request L-GB resident mem
endselect
print *, "allocate() passed (type any key)"
read *
end
- Linux (x86_64) 64 64 . ulimit -v "". (method= 1,2,3) L > ~ 120, .. . method= 1,3
Operating system error: Cannot allocate memory
Allocation would exceed memory limit
method= 2, stat=ierr . L 120 , method= 2 0... , , allocate(), -, + ( ), ulimit -v .
, allocate(), ulimit -v. 4 2 .
program alloc_test
implicit none
real, allocatable :: a(:), b(:)
integer ierr, n
n = 500000000
allocate( a( n ), stat=ierr ) !! requests 2GB virtual memory
if ( ierr /= 0 ) stop "Memory error! (a)"
allocate( b( n ), stat=ierr ) !! requests 2GB virtual memory
if ( ierr /= 0 ) stop "Memory error! (b)"
print *, "before assignment (type any key)"
call system( "ps aux | grep a.out" )
read *
print *, "now writing values..."
a(:) = 0.0 !! request 2GB resident memory
print *, "after assignment (type any key)"
call system( "ps aux | grep a.out" )
read *
end
./a.out , allocate(). 1GB
$ ( ulimit -v 1000000 ; ./a.out )
STOP Memory error! (a)
2,2
STOP Memory error! (b)
Finally, if we install it on> 4 GB, the assignment begins
before assignment (type any key)
<username> 12380 0.0 0.0 3918048 652 pts/1 S+ 07:59 0:00 ./a.out
now writing values...
after assignment (type any key)
<username> 12380 38.0 2.9 3918048 1953788 pts/1 S+ 07:59 0:00 ./a.out
Thus, we can limit the amount of virtual memory (if necessary) to allocate( ..., stat=ierr )cause an error with respect to general purpose.