LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How Linux allocates memory for malloc in a program (http://www.linuxquestions.org/questions/linux-newbie-8/how-linux-allocates-memory-for-malloc-in-a-program-457415/)

johnarg 06-22-2006 08:54 PM

How Linux allocates memory for malloc in a program
 
Hi,
I am new to Linux. I am allocating memory using malloc in a C program.
****************************
int *ptr;
if((ptr = malloc(sizeof(int)*1024*2000)) == NULL)
{
fprintf(stdout, "Malloc failed\n");
}

while(1)
{
//To make program running continuosly
}
******************************
When I check the memory being used by the system for this process using top command, it shows 308KB(RSS).
Could someone help me, why the difference occured in the memory allocated in the program using malloc and the memory allocated by the System.
I want to know how Linux allocates memory for a process.

Please help ASAP. Thanks in advance.

FLLinux 06-24-2006 07:52 AM

I would have to look into who linux allocates memory but I would warn you that it looks like you are mallocing about 8 Megs of memory. That is kind of risky with malloc since it does not do a check that something else is using that memory and you could write over other program memory space. Just a warning.

johnarg 06-25-2006 07:15 PM

Quote:

Originally Posted by FLLinux
I would have to look into who linux allocates memory but I would warn you that it looks like you are mallocing about 8 Megs of memory. That is kind of risky with malloc since it does not do a check that something else is using that memory and you could write over other program memory space. Just a warning.



Thank you very much for ur suggestion. However, I want to check a program for Memory usage. I am checking it with RSS of top command. However, I can I be sure that RSS shows actual memory usage. If u could pls. suggest something, pls. advice me.
Thanking u in advance.


All times are GMT -5. The time now is 11:26 AM.