Why is there a error “Cannot Allocate Memory” while creating message queue in POSIX?
Why is there a error Cannot Allocate Memory while creating message queue in POSIX?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Adrian’s answer is correct, but since this is a frustratingly common error to run into on Linux when first attempting to use POSIX message queues for anything non-trivial, I thought I’d add some helpful particulars.
First, to understand the
RLIMIT_MSGQUEUEresource limit, see the formula atman setrlimit:Given the default MQ settings (
mq_maxmsg= 10,mq_msgsize= 8192) on Linux, the above formula works out to only about 10 message queues for the default limit of 819200 bytes. Hence why you will run into this problem as soon as you e.g. forget to close and unlink a couple of queues once done with them.To raise the
RLIMIT_MSGQUEUEresource limit to the maximum allowed for the user, you can use something like the following in your application’s startup code:If you also ensure that you set the
mq_maxmsgandmq_msgsizeattributes to lower values when opening a queue (seeman mq_open), you may be able to get away with a couple of hundred queues even within the constraints of the defaultRLIMIT_MSGQUEUEhard limit. Depending on your particular use case, of course.Adjusting the
RLIMIT_MSGQUEUEhard limit is not difficult if you have root access to the system. Once you’ve figured out what the limit ought to be, adjust the system-wide settings in/etc/security/limits.conf. For example, to set a hard and soft limit of 4 megabytes for thewww-datauser group, and no limitation for the superuser, you’d add the following lines to the file: