Skip to content
Advertisement

Getting cannot allocate memory error

I am getting this error in my program…

JavaScript

ulimit -a gives the output:

JavaScript

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?

Advertisement

Answer

JavaScript

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.

Advertisement