tags:

views:

48

answers:

1

I do not know if this the correct forum to ask this question but I am a fan of stackoverflow and so have decided to go ahead posting it here.

If I output the /proc//smaps, I find several segments which do not have any name associated with them and also with inode number as 0. As per the linux kernel Documentation, 0 indicates that no inode is associated with the memory region, as the case would be with BSS (uninitialized data).

I tried searching for BSS but could not get exactly what it is. The information that I got is that BSS is that segment of memory responsible for unititalised global and static variables.

My question is what else does memory region with inode number 0 contain?

I wrote a C program in which I dd the following:- (i) Malloc 4 Mb for an array on integers (ii) Cat /proc//smaps (iii) Found an added memory segment with inode number "0" in the smaps. (iv) Inittialised some part of this array to 5.

STILL found that this memory segment is attached with inode number 0 only. Another question is when does this memory segment get converted to heap?

A: 

The mappings with inode number 0 are anonymous mappings - essentially those that have been created with the MAP_ANONYMOUS flag to mmap().

This just means that they aren't associated with a disk file. The inode number isn't going to change though; it'll always stay as 0 for that mapping.

Anonymous mappings don't get converted to heap. In fact "[heap]" is just a convenience marker for the anonymous mapping that is set up by the kernel at exec time and altered by the brk() system call.

caf
So when and how exactly does this anonymous mapping gets changed to Heap Memory segment?
Vaibhav
It doesn't. What makes you think that it does?
caf
I did a malloc() of 4 Mb into array of integers. I believe this memory should go into Heap Segment. But from the output of smaps it was clear that it went to the anonymous memory region. I do understand that since the program I am running is a user mode process so an equivalent RAM of 4 Mb would not be allocated i.e. 4 Mb of Virtual Address Space would be mapped to the Annonymous Memory region. However wont this memory region once initialised should be marked as "Heap Segment" as this is dynamic memory allocation? If not why and then what does heap segment contain?
Vaibhav
No, it doesn't. As I mentioned in the answer, the mapping marked as "[heap]" is just indicating a special anonymous mapping - the one that is manipulated by the `brk()` system call. This used to be the way that `malloc()` always acquired memory - but these days, `malloc()` often creates a new anonymous mapping (as you've seen in your example). Small allocations (eg a few hundred bytes) made with `malloc()` will likely be made within the `[heap]` mapping.
caf
Yes. Indeed it is that way. Thanks
Vaibhav