2017-04-13 129 views
2

我想在PHP腳本中測試內存分配。我在php.ini中將限制設置爲128M。在腳本中,ini_get('memory_limit')報告128M爲限制。問題是,當我開始分配空間時,我無法超越23M。我可以將內存分配限制在23M以下的任何級別,但不能分配更多內存。只是尋找這個限制在哪裏被施加的線索。PHP內存限制

這裏是我的測試腳本...

<?php 

echo "System imposed memory_limit is set to: " . ini_get('memory_limit') . "<br>\n"; 
for ($i=1; $i<1000; $i++) { 
    $a = loadmem($i); 
    echo "You have allocated ". $i . "M (". memory_get_usage() . ") memory in this php script" . "<br />\n"; 
    unset($a); 
} 

function loadmem($howmuchmeg) { 
    $a = str_repeat("0", $howmuchmeg * 1024 * 1024); // alocating 10 chars times million chars 
    return $a; 
} 

?> 

這裏的使用curl輸出...

System imposed memory_limit is set to: 128M<br> 
You have allocated 1M (1403632) memory in this php script<br /> 
You have allocated 2M (2452232) memory in this php script<br /> 
You have allocated 3M (3500808) memory in this php script<br /> 
You have allocated 4M (4549384) memory in this php script<br /> 
You have allocated 5M (5597960) memory in this php script<br /> 
You have allocated 6M (6646536) memory in this php script<br /> 
You have allocated 7M (7695112) memory in this php script<br /> 
You have allocated 8M (8743688) memory in this php script<br /> 
You have allocated 9M (9792264) memory in this php script<br /> 
You have allocated 10M (10840848) memory in this php script<br /> 
You have allocated 11M (11889424) memory in this php script<br /> 
You have allocated 12M (12938000) memory in this php script<br /> 
You have allocated 13M (13986576) memory in this php script<br /> 
You have allocated 14M (15035152) memory in this php script<br /> 
You have allocated 15M (16083728) memory in this php script<br /> 
You have allocated 16M (17132304) memory in this php script<br /> 
You have allocated 17M (18180880) memory in this php script<br /> 
You have allocated 18M (19229456) memory in this php script<br /> 
You have allocated 19M (20278032) memory in this php script<br /> 
You have allocated 20M (21326608) memory in this php script<br /> 
You have allocated 21M (22375184) memory in this php script<br /> 
You have allocated 22M (23423760) memory in this php script<br /> 
You have allocated 23M (24472336) memory in this php script<br /> 

下面是相關的error_log條目...

mmap() failed: [12] Cannot allocate memory 
PHP Fatal error: Out of memory (allocated 2097152) (tried to allocate 25165856 bytes) 

使用php 7.0.x和apache2.2

+0

你能提供你的環境的詳細信息?我只是在Docker運行laravel的修補程序('docker run -it -rm eboraas/laravel php/var/www/laravel/artisan tinker')中嘗試了你的示例代碼,並設法迭代到'110M'。 – m4grio

+0

謝謝。問題是apache用戶的內存限制。 –

回答

5

該錯誤表示您實際上沒有可用的內存。如果你只是簡單地超過了限制,你會得到一個稍微不同的訊息:

PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried... 

你的問題可能是實際可用內存結果或殼強加的ulimit。

+0

這很混亂。免費-m顯示203M免費,但當我嘗試分配25M時,我得到了致命的php錯誤。 –

+0

'free -m'不考慮'ulimit'。 –

+0

我想'ulimit -a'會告訴你目前所定義的限制是什麼... –

0

添加此喲YOUT PHP腳本

<?php 

ini_set('memory_limit', '2048M'); 

?> 
+0

給了一個鏡頭。同樣的結果。 –

+0

爲它增加更多內存,如** ini_set('memory_limit','1000000000000000M'); ** –