inapathtolearn
inapathtolearn

Reputation: 75

mlockall(): cannot allocate memory

I am getting the error: mlockall failed: Cannot allocate memory when calling the function mlockall():

if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
            printf("mlockall failed: %m\n");
            exit(-2);
    }

IDE used: Eclipse OS: Ubuntu 16.04 Real time extension using : 4.11.12 rt-15 (preempt patch) Note : I am trying to so a UDP CONNECTION in realtime (using intel i210 ethernet controller).

When i debug the same in eclipse this is what is showing:

Can't find a source file at "/build/glibc-bfm8X4/glibc-2.23/misc/../sysdeps/unix/syscall-template.S" Locate the file or edit the source lookup path to include its location.

How to solve this?

Upvotes: 3

Views: 8241

Answers (1)

Sam Nobs
Sam Nobs

Reputation: 71

As man 2 mlockall explains you don't seem to have the permissions to lock the amount of memory that you tried to lock. You can either adjust that limit, or try and run your program as root, e.g. via sudo. I don't recommend doing the latter for anything other than finding out if the limits are really the problem, so here's how to adjust the limits for bash on Kubuntu - it could work differently on your distro.

First, let's see what your limit is:

$ ulimit -a | grep locked
max locked memory       (kbytes, -l) 64

64 kbytes isn't a lot! There's a shorthand, for this, too:

$ ulimit -l
64

More than likely you won't be able to adjust the limit easily:

$ ulimit -l 1000000
bash: ulimit: max locked memory: cannot modify limit: Operation not permitted

And even if you were, it wouldn't persist across shell invocations, reboots etc. So we need a different approach.

/etc/security/limits.conf to the rescue! This is the file the default values for the limits are read from when a user session is started - see man 5 limits.conf.

Add the following line (nobss is my user name, you should use yours, obviously)

nobss - memlock 1000000

to the file to set both the hard and soft limits to 1000000 kbytes. Log out and login in, and voilà:

$ ulimit -l
1000000

That's better. Unless your program uses heaps of memory this limit should do - in fact it's more than enough in most cases.

Upvotes: 7

Related Questions