# Shared Memory Structure?

**URL:** <https://www.ruby-forum.com/t/shared-memory-structure/244558>\
**Category:** NGINX\
**Created:** [May 3, 2016, 11:45am UTC](https://www.ruby-forum.com/t/shared-memory-structure/244558 "2016-05-03T11:45:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [May 3, 2016, 11:45am UTC](https://www.ruby-forum.com/t/shared-memory-structure/244558/1 "2016-05-03T11:45:32Z")

</div>

I am storing things in a shared memory zone allocated with  
ngx\_shared\_memory\_add and allocating each slab with  
ngx\_slab\_alloc\_locked  
(as these items don’t need to be locked particularly). The problem I’m  
having is that I add the shared memory with exactly the amount I need.  
However, the final large thing to be allocated fails, but after this a  
lot  
more smaller things are also allocated without any problem. There is  
enough  
space for it but it has a problem with a large chunk when space is  
getting  
tight.  
Does anyone know why this is or how to fix it? My guess was that nginx  
structures the shared memory in a way that means there is enough memory  
(which I have made sure there is) but it is not continuous.  
Thanks.

Posted at Nginx Forum:

> **[Shared Memory Structure?](https://forum.nginx.org/read.php?2,266569,266569#msg-266569)**
>
> I am storing things in a shared memory zone allocated with ngx\_shared\_memory\_add and allocating each slab with ngx\_slab\_alloc\_locked (as these items don’t need to be locked particularly). The problem I'm having is that I add the shared memory with...

---

<div class="post-metadata">

**Author:** ![ben5192](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e8c25b/32.png) [@ben5192](https://www.ruby-forum.com/u/ben5192)\
**Post date:** [May 4, 2016, 3:41pm UTC](https://www.ruby-forum.com/t/shared-memory-structure/244558/2 "2016-05-04T15:41:31Z")

</div>

Hello!

On Tue, May 03, 2016 at 05:45:05AM -0400, ben5192 wrote:

> (which I have made sure there is) but it is not continuous.  
> Thanks.

Shared memory size as specified in a ngx\_shared\_memory\_add() call is the  
total size of the memory region to be allocated by nginx. As long  
as you use it with slab allocator - various slab allocator  
structures are allocated in this memory region, starting with  
the ngx\_slab\_pool\_t structure, and followed by multiple  
ngx\_slab\_page\_t structures.

If you want to use shared memory for an allocation with a known  
size, you have to add some memory to account these internal  
structures. Note that it may be not trivial to calculate how may  
extra memory you’ll need - see the slab allocator code for  
details. Adding at least 8 \* ngx\_pagesize should be a good  
idea.

–  
Maxim D.

> **[nginx](http://nginx.org/)**
