LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2006, 11:43 PM   #1
coming
LQ Newbie
 
Registered: Jun 2006
Posts: 1

Rep: Reputation: 0
Thumbs up Linux memory problem


I am trying to allocate memory to linking list dynamically.
When I call the free function, the memory is not actually freed.
The consumed memory profile are as follows:

Before free
root 9031 2.6 5.2 99432 40592 pts/2 S+ 12:16 0:00 ./a.out

After free
root 9031 2.6 5.2 99432 40592 pts/2 S+ 12:16 0:00 ./a.out

However, if I exeucted the above codes on Windows 2000, the memory will be actually freed.
Would you guys have any comment?

my source code is as follows:
Code:
//----------------------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct _node
{ 
    int i;
    char *parray;
    struct _node *next;

}Node;

Node* newNode(int i);
void freeNode(Node *n);
void freeNodeLoop(Node *n);

int main()
{
    int j;
    // allocate 10000 nodes
    Node *head = newNode(10000);
    while(1)
    {
        int i;
        printf("push 1 to free memory, 2 to exit\n");
        scanf("%d", &i);
        if( i==1 )
        {
            printf("free node\n");
            freeNode(head);	
            head = NULL;
        }
	else if ( i == 2)
            break;
    }
    return 0;
}

Node* newNode(int i)
{
    Node *head;
    Node *pre;
    Node *cur;
    int k;

    head = NULL;
    pre = NULL;

    for(k=0; k<i; k++)
    {
        cur = (Node*)malloc(sizeof(Node));
	cur->parray = (char*)malloc(sizeof(char)*10000);
	cur->i = k;
	cur->next = NULL;
       	
	if( head == NULL )
       	{
       	    head = cur;
       	}
       	else
       	{
 	    pre->next = cur;
       	}
       	pre = cur;
    }
    
    return head;
}

void freeNode(Node *n)
{
    if( n == NULL)
    {
        printf("Node is NULL\n");
       	return;
    }

    if(n->next != NULL)
        freeNode(n->next);
    free(n->parray);
    free(n);
}
//---------------------------------

Last edited by coming; 06-14-2006 at 12:04 AM.
 
Old 06-14-2006, 12:24 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
malloc/free implementations rarely return freed memory to the O/S, and in fact some of them never do it.
The common behaviour is to make the memory available for future mallocs.
 
Old 06-14-2006, 12:30 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
When you malloc and free memory the programs reported memory footprint often does not change. The first time you allocate dynamic memory the system typically gives you way more then you ask for (it always rounds to a full page and I think the minimum it gives you is two pages). Subsequent mallocs end up using the same memory that was allocated for the process the first time until there is none left there, and then it will do a real allocation again. The reason for this is actually going through the process of getting "new" memory for your process is very expense (time wise). This is also why when you free it doesn't give it up immediately.

Rest assured, the memory is reusable if need be.
 
Old 06-14-2006, 08:49 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Actually, when GNU libc malloc implementation is used, memory freed will be guaranteed to be released by the O/S and consequently decrease the (virtual) memory footprint of a process only if it was allocated by mmap and not on the heap by brk.
This happen when the allocated block size is bigger than M_MMAP_THRESHOLD.
The heap is released only the chunk is bigger than M_TRIM_THRESHOLD.

On BSD, Solaris and most other Unix malloc implementations, no such distinction exists, memory is allocated on the heap, and never released.

This is not a problem as long as you are not short of swap space.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with shared memory in C under Linux d1s4st3r Programming 4 06-29-2005 03:34 AM
linux memory problem & mail quota Ammad Linux - Newbie 1 06-10-2004 06:01 AM
A linux memory problem jack1234 Linux - Newbie 0 03-02-2004 12:16 PM
Help! Linux memory problem soowk Linux - Newbie 2 06-07-2003 08:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration