本文介绍了越来越无法分配内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误在我的计划...

 则mprotect:无法分配内存

的ulimit -a 的给出的输出:

 核心转储文件大小(块,-c)0
数据赛格大小(千字节,-d)无限
调度优先级(-e)20
文件大小(块,-f)无限
挂起信号(-i)16382
最大锁定内存(千字节,-l)64
最大内存大小(千字节,-m)无限
打开文件(-n)1024
管道尺寸(512字节,-p)8
POSIX消息队列(字节,-q)819200
实时优先级(-r)0
堆栈大小(千字节,-S)8192
CPU时间(秒,-t)无限
最大用户进程(-u)无限
虚拟内存(​​千字节,-v)无限
文件锁(-x)无限

我试图保护的内存量为60 MB。谁能告诉我是什么问题以及如何解决?


解决方案

返回值如果成功,则mprotect()返回零。
在错误,则返回-1,并且errno设置为合适。错误EACCES
该内存不能被赋予指定的访问。这可能发生,例如,如果
你的mmap(2)的文件到你只读访问,然后问mprotect()对来
它标记PROT_WRITE。EFAULT
的存储器不能被访问。EINVAL
addr是不是有效的指针,或不PAGESIZE的倍数。ENOMEM
内部内核结构不能进行分配。
或者:在范围内的地址[地址,ADDR + LEN]是为的地址空间无效
的过程中,或指定未映射的一个或多个页面。

由于错误信息,你可能有一个 ENOMEM 错误,看着错误code,这并不一定意味着内存不能进行分配。你可能有一个无效的地址范围,或(最有可能),你有没有被映射的页面。

不要试图保护的内存这样的大​​帅哥在一个膨胀foop。鉴于虚拟内存是如何工作的,赔率是太高了,在这大块有些页面将不会被映射。您需要确保该页面(页面)有关呼吁的mprotect前映射。

当你正在使用的系统功能它始终是读该函数的手册页是个好主意。然后重新阅读。该手册页有时可能有点简洁。

I am getting this error in my program...

mprotect: Cannot allocate memory

ulimit -a gives the output:

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

The amount of memory I'm trying to protect is 60 MB. Can someone tell me what is the problem and how it can be solved?

解决方案
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.

Given the error message, you probably got an ENOMEM error, and looking at the error code, this does not necessarily mean that memory could not be allocated. You might have an invalid address range, or (most likely) you have pages that aren't mapped.

Don't try to protect such a big hunk of memory in one swell foop. Given how virtual memory works, the odds are just too high that some page in that huge chunk will not be mapped. You need to ensure that the page (pages) in question are mapped before calling mprotect.

When you are using system functions it is always a good idea to read the man page on that function. Then re-read it. The man pages can be a bit terse at times.

这篇关于越来越无法分配内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 11:29