Results 1 to 4 of 4
Hello all,
I am new to this community. I want to know what happens exactly when malloc is called. I don't worry about which function is called internally by malloc. ...
- 06-12-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 10
malloc no change in vsz
Hello all,
I am new to this community. I want to know what happens exactly when malloc is called. I don't worry about which function is called internally by malloc. I am interested in knowing how memory allocated by malloc is mapped to physical ram.
I have tried a simple example which does the following in main:
getchar();
while(1)
{
char* ptr = (char*)malloc(4*1024);
getchar();
free(ptr);
getchar();
}
For the first allocation, vsz in ps output increased, but for all the next it remained constant.
Sorry if my understanding is not right.
Thanks in advance.
- 06-12-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The allocator uses a system function sbrk to get more heap space from the operating system. It then manages the local heap. When you malloc() some memory the first time, sbrk() is called to get enough memory (and then some) from the OS, which it passes back to the caller. When that memory is freed, it is tagged as available for future allocations. In your case, you are allocating/freeing the same chunk of memory, so the system has had no need to allocate more memory for your process.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-16-2009 #3Just Joined!
- Join Date
- Jun 2009
- Posts
- 10
Hi,
Nice to see you here too..
I got what you are saying. But my basic doubt is, i have allocated 4kb which is not reflected in ps output(vsz parameter). I mean to say for the first time when i allocated 4kb the increase was very high, what could be the reason? Also the memory allocated is from ram or disk. I want to know exactly how memory is mapped from malloc call to physical device. Sorry if my question or understanding is wrong. Also forgive me if this sounds odd...
- 06-16-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
I'm not sure offhand how much sbrk() will pull from the operating system at a time. It's somewhat more than you are asking for, I think. Since Linux is a virtual memory system, when you use the memory, it is in RAM. When you are not using it, it might be swapped to disc. If you are writing a kernel module, then you can tell if a memory address that belongs to a process is swapped or not, and you can force it into ram and pin it there for kernel operations, such as moving a message from the network or other device into the program's memory. Remember that since this is virtual memory, until the something actually tries to access it, it doesn't exist! There are some good treatises on the net that explain how the Linux memory system works. Certainly better than I can provide in a thumbnail here. Go to The Linux Kernel Archives or The Linux Documentation Project for a lot of this sort of information.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
