簡體   English   中英

獲取無法分配內存錯誤

[英]Getting cannot allocate memory error

我的程序中出現此錯誤...

mprotect: Cannot allocate memory 

ulimit -a給出輸出:

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

我要保護的內存量為60 MB。 有人可以告訴我問題是什么以及如何解決?

Return Value

On success, mprotect() returns zero.
On error, -1 is returned, and errno is set appropriately.

Errors

EACCES
The memory cannot be given the specified access. This can happen, for example, if
you mmap(2) a file to which you have read-only access, then ask mprotect() to
mark it PROT_WRITE.

EFAULT
The memory cannot be accessed.

EINVAL
addr is not a valid pointer, or not a multiple of PAGESIZE.

ENOMEM
Internal kernel structures could not be allocated.
Or: addresses in the range [addr, addr+len] are invalid for the address space of
the process, or specify one or more pages that are not mapped.

給出錯誤消息后,您可能會收到一個ENOMEM錯誤,並查看錯誤代碼,這並不一定意味着無法分配內存。 您的地址范圍可能無效,或者(很可能)您的頁面沒有映射。

不要試圖一口氣地保護這么大的內存。 考慮到虛擬內存的工作原理,其可能性太大,以致無法映射該大塊中的某些頁面。 您需要確保在調用mprotect之前已映射有問題的頁面。

使用系統功能時,最好閱讀該功能的手冊頁。 然后重新閱讀。 手冊頁有時可能會很簡潔。

盡管我認為這不是您的問題,但還應注意,由於分配失敗, mprotect肯定會失敗,原因至少有兩個:

  1. 如果僅在VMA的一部分上更改了權限,則內核有必要將其拆分為兩個VMA。 此拆分需要分配資源,並且可能會失敗。 (請注意,由於同樣的原因, munmap也會失敗!)
  2. 將非臟頁從只讀狀態更改為可寫狀態時,這會增加該過程的提交費用。 在嚴格禁止過量使用的系統上,當物理內存/交換用盡時,提交費用的這種“分配”可能會失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM