获取无法分配内存错误 [英] Getting cannot allocate memory error

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

问题描述

我的程序中遇到此错误...

  mprotect:无法分配内存



ulimit -a 提供输出:

 核心文件大小(块,-c)0 
数据段大小(kbytes,-d)无限
调度优先级(-e)20
文件size(blocks,-f)unlimited
pending信号(-i)16382
最大锁定内存(kbytes,-l)64
最大内存大小(kbytes,-m)unlimited
打开文件(-n)1024
管道大小(512字节,-p)8
POSIX消息队列(字节,-q)819200
实时优先级b $ b堆栈大小(kbytes,-s)8192
cpu时间(秒,-t)无限
最大用户进程(-u)unlimited
虚拟内存(kbytes,-v)
文件锁(-x)无限

我试图保护的内存量是60 MB。有人可以告诉我这是什么问题以及如何解决?

解决方案

 返回值

成功时,mprotect()返回零。
出错时,返回-1,并且适当地设置errno。

错误

EACCES
内存不能被指定访问。这可能会发生,例如,如果
你mmap(2)一个文件,你有只读访问,然后问mprotect()到
标记它的PROT_WRITE。

EFAULT
无法访问内存。

EINVAL
addr不是有效的指针,或不是PAGESIZE的倍数。

ENOMEM
无法分配内部内核结构。
或者:范围[addr,addr + len]中的地址对于进程的
的地址空间无效,或指定一个或多个未映射的页面。

给出错误消息,您可能得到 ENOMEM 错误,并查看错误代码,这并不一定意味着内存不能分配。您可能拥有无效的地址范围,或者(很可能)您有未映射的网页。



不要尝试保护这么大的内存块一个肿胀。给定虚拟内存如何工作,可能性太高,以致于该大块中的某些页面将不会被映射。您需要确保在调用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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆