Free() and size of the process

P

pushpakulkar

Hi all,

Suppose you have simple piece of code something like this. It is only
for testing purpose, please pardon
syntax errors.

#include <malloc.h>
#include <stdio.h>

int main()
{
int k=1;
int *p;

if(k)
{
p=(int *)malloc(1024*1024);
if(p)
{
printf("Malloc successful\n");

//Point A - Do something .....................
// .........................................

free(p);


//Point B - Do something
else .........................
//......................................................
}
return 1;
}

My question is that at Point B, size of process will be say 1872K(on
my system). After free(p) at Point B, size of the process will be say
around 872K. Though the freed up memory is showing up on the size of
process, I have heard that some processes don't really return the
memory to the OS, rather it is marked as free() and will be released
to the operating system only after the program exits. Also this memory
can be released when there is a requirement. If the process doesn't
return the memory to the operating system, does it not reflect on the
size of the process. Can someone please give some feedback on the
same.

Regards,
Push
 
B

Barry Schwarz

Hi all,

Suppose you have simple piece of code something like this. It is only
for testing purpose, please pardon
syntax errors.

#include <malloc.h>

This is a non-standard header. malloc should be properly declared in
stdlib.h.
#include <stdio.h>

int main()
{
int k=1;
int *p;

if(k)
{
p=(int *)malloc(1024*1024);

Don't cast the return from malloc.
if(p)
{
printf("Malloc successful\n");

//Point A - Do something .....................
// .........................................

free(p);


//Point B - Do something
else .........................
//......................................................
}
return 1;
}

My question is that at Point B, size of process will be say 1872K(on
my system). After free(p) at Point B, size of the process will be say

What is a process? The term is not used in the standard in relation
to a running program.

What standard C function provided this information?
around 872K. Though the freed up memory is showing up on the size of
process, I have heard that some processes don't really return the
memory to the OS, rather it is marked as free() and will be released
to the operating system only after the program exits. Also this memory

This is system specific and probably related to your run time library
rather than your particular program.
can be released when there is a requirement. If the process doesn't
return the memory to the operating system, does it not reflect on the
size of the process. Can someone please give some feedback on the
same.

You really to ask system specific questions is a group where your
system is topical.
 
S

S M Ryan

to the operating system only after the program exits. Also this memory
can be released when there is a requirement. If the process doesn't
return the memory to the operating system, does it not reflect on the
size of the process. Can someone please give some feedback on the
same.

If you're working with virtual memory like most people today, don't worry about
it. Idle heap pages get paged out and only exist on disk. And the amount disk
used for vm is going to be miniscule on today's hundred gigabyte disks.

Don't worry about it unless you really are getting disk full errors.

If you don't have vm, you will have to depend on system specifics to manage real
memory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top