Linux

Apache strace 尋找記憶體洩漏

  • December 14, 2012

我們有一個存在記憶體問題的伺服器:伺服器一直為自己分配記憶體並且不釋放它。我們正在執行 Apache。我將 MaxReqsPerClient 設置為一個非常低的值,這樣執行緒就不會佔用很多記憶體,但是有人見過這樣的呼叫嗎?我認為這可能是 Drupal 從數據庫中的記憶體中拉回了太多數據,我錯了嗎?

read(52, "h_index\";a:2:{s:6:\"weight\";i:1;s"..., 6171) = 1368
read(52, "\";a:2:{s:6:\"author\";a:3:{s:5:\"la"..., 4803) = 1368
read(52, ":\"description\";s:19:\"Term name t"..., 3435) = 1368
read(52, "abel\";s:4:\"Name\";s:11:\"descripti"..., 2067) = 1368
read(52, "ions\";a:2:{s:4:\"form\";a:3:{s:4:\""..., 16384) = 708
brk(0x2ab554396000)                     = 0x2ab5542f5000
mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ab55f653000
brk(0x2ab554356000)                     = 0x2ab5542f5000
mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ab55f753000
brk(0x2ab554356000)                     = 0x2ab5542f5000
mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ab55f853000
brk(0x2ab554356000)                     = 0x2ab5542f5000
mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ab55f953000
brk(0x2ab554356000)                     = 0x2ab5542f5000
mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ab55fa53000
brk(0x2ab554356000)                     = 0x2ab5542f5000
mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ab55fb53000
brk(0x2ab554356000)                     = 0x2ab5542f5000
mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2ab55fc53000
poll([{fd=52, events=POLLIN|POLLPRI}], 1, 0) = 0 (Timeout)
write(52, "d\0\0\0\3SELECT cid, data, created, "..., 104) = 104
read(52, "\1\0\0\1\5E\0\0\2\3def\23drupal_database_nam"..., 16384) = 1368
read(52, ";s:11:\"granularity\";a:5:{s:4:\"ye"..., 34783) = 1368
read(52, ":4:\"date\";}s:9:\"datestamp\";a:9:{"..., 33415) = 1368
read(52, "\";i:0;s:15:\"display_default\";i:0"..., 32047) = 1368
read(52, "e as an integer value.\";s:8:\"set"..., 30679) = 1368
read(52, "label' pairs, i.e. 'Fraction': 0"..., 29311) = 1368

頂部(procs 只是在記憶體中不斷增長..):

12845 apache    15   0  581m 246m  37m S  0.0  4.1   0:17.39 httpd                                   
12846 apache    15   0  571m 235m  37m S  0.0  4.0   0:12.13 httpd                                   
12833 apache    15   0  420m 117m  37m S  0.0  2.0   0:06.04 httpd                                   
12851 apache    15   0  412m 113m  37m S  0.0  1.9   0:05.32 httpd                                   
13871 apache    15   0  409m 109m  37m S  0.0  1.8   0:04.90 httpd                                   
12844 apache    15   0  407m 108m  37m S  0.0  1.8   0:04.50 httpd                                   
13870 apache    15   0  407m 108m  37m S  0.3  1.8   0:03.50 httpd                                   
14903 apache    15   0  402m 103m  37m S  0.3  1.7   0:01.29 httpd                                   
14850 apache    15   0  397m 100m  37m S  0.0  1.7   0:02.08 httpd                                   
14907 apache    15   0  390m  93m  36m S  0.0  1.6   0:01.32 httpd                                   
13872 apache    15   0  386m  91m  37m S  0.0  1.5   0:03.13 httpd                                   
12843 apache    15   0  373m  81m  37m S  0.0  1.4   0:02.51 httpd                                   
14901 apache    15   0  370m  75m  33m S  0.0  1.3   0:00.78 httpd                                   
14904 apache    15   0  335m  29m  15m S  0.0  0.5   0:00.26 httpd  

我最終只是降低了 httpd.conf 中的 MaxRequestsPerChild,所以我的執行緒在它們變得太大之前就過期了。他們之前達到了 800M 並且有一些嚴重的交換,現在他們在死亡之前最多達到 250M。

這是在什麼時間段內發生的?記憶體使用量有多大?

如果時間足夠短,或者您暫時沒有在前台執行 Apache,您可以使用valgrind來查找記憶體洩漏…(可通過 YUM 獲得)

就像是:

valgrind --leak-check=full /usr/sbin/httpd 

輸出可能會顯示如下內容:

==945== LEAK SUMMARY:
==945== definitely lost: 15,595 bytes in 120 blocks
==945== indirectly lost: 1,917,248 bytes in 1,242 blocks
==945== possibly lost: 0 bytes in 0 blocks
==945== still reachable: 11,743 bytes in 89 blocks
==945== suppressed: 0 bytes in 0 blocks
==945== Reachable blocks (those to which a pointer was found) are not shown.
==945== To see them, rerun with: --leak-check=full --show-reachable=yes

您可以使用pmap實用程序查看程序的記憶體分配。

對於 Apache,我會看一下 PID。使用以下命令獲取 PID 值:cat /var/run/httpd.pid或執行:

pmap -x $(cat /var/run/httpd.pid)

但在我有限的 Apache 經驗中,洩漏似乎總是與模組和腳本語言相關聯。也可能是記憶體…

引用自:https://serverfault.com/questions/457194