Thread: Deleting objects show no drop in memory usage.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23

    Deleting objects show no drop in memory usage.

    Hello, I've been working on a custom linked list for objects. The problem is I am deleting objects in the linked list, however it is having no effect on the memory usage.

    I do not recieve any compiler, linker, or runtime errors. I don't recieve any errors relating to memory either.

    Object class and associated functions:
    Code:
    class Obj
    {
    public:
    	Obj(int iII=0)
    	{
    		pcNext=NULL;
    		pcPrev=NULL;
    		iI=iII;
    	}
    	~Obj()
    	{
    	}
    	//All static functions relate to the list itself; they can be accessed by typing Obj::NameOfFunc
    	static Obj* listGetObj(int iIndex);
    	static int listGetIndex(Obj* pcObj);
    	static int listAdd(Obj* pcObj,int iWhere=-1);
    	static bool listRemove(int iIndex);
    	static void sayAll();
    
    	static Obj* pcStart;
    	static Obj* pcEnd;
    	static sui count;
    
    	void sayI()
    	{
    		cout << iI << "\n";
    	}
    private:
    	Obj* pcNext;
    	Obj* pcPrev;
    	int iI;
    };
    sui Obj::count=0;
    Obj* Obj::pcStart=NULL;
    Obj* Obj::pcEnd=NULL;
    
    Obj* Obj::listGetObj(int iIndex)
    {
    	if(iIndex<0||iIndex>=count)
    	{
    		return NULL;
    	} else {
    		Obj* pcTarg=pcStart;
    		for(int i=0; i<=iIndex; i++)
    		{
    			if(i==iIndex&&pcTarg!=NULL)
    				return pcTarg;
    			else
    				pcTarg=pcTarg->pcNext;
    		}
    		return pcTarg;
    	}
    }
    
    int Obj::listGetIndex(Obj* pcObj)
    {
    	return 0;
    }
    
    int Obj::listAdd(Obj* pcObj,int iWhere)
    {
    	int index=iWhere;
    
    	//If -1 set place to add to end of list.
    	if(index<0)
    		index=count;
    	
    	//If index is end of list
    	if(index>=count)
    	{
    		if(pcEnd!=NULL)
    		{
    			pcEnd->pcNext=pcObj;
    			pcObj->pcPrev=pcEnd;
    		}
    		pcEnd=pcObj;
    	}
    
    	//If index is start of list - keep in mind if list is empty the new item is both the start and end.
    	if(index==0) {
    		if(pcStart!=NULL) {
    			pcStart->pcPrev=pcObj;
    			pcObj->pcNext=pcStart;
    		}
    		pcStart=pcObj;
    	}
    	if(index!=0&&index<count)
    	{
    		Obj* pcMove=listGetObj(index);
    		if(pcMove!=NULL) {
    			pcObj->pcPrev=pcMove->pcPrev;
    			pcObj->pcNext=pcMove;
    			pcMove->pcPrev->pcNext=pcObj;
    			pcMove->pcPrev=pcObj;
    		} else {
    			return -1;
    		}
    	}
    	count++;
    	return index;
    }
    
    bool Obj::listRemove(int iIndex)
    {
    	//Get Object and Check Validity
    	Obj* pcRemove=listGetObj(iIndex);
    	if(pcRemove==NULL)
    		return false;
    
    	//If removing the first
    	if(pcRemove==pcStart) {
    		pcStart=pcRemove->pcNext;
    	}
    	//If removing the last
    	if(pcRemove==pcEnd) {
    		pcEnd=pcRemove->pcPrev;
    	}
    
    	//Bridge gaps caused removed link. Set the next link's last to last and the last link's next to next.
    	if(pcRemove->pcNext!=NULL) {
    		pcRemove->pcNext->pcPrev=pcRemove->pcPrev;
    	}
    	if(pcRemove->pcPrev!=NULL) {
    		pcRemove->pcPrev->pcNext=pcRemove->pcNext;
    	}
    	
    	delete pcRemove;
    	count--;
    
    	return true;
    }
    
    void Obj::sayAll()
    {
    	for(Obj* pcCurrent=pcStart; pcCurrent!=NULL; pcCurrent=pcCurrent->pcNext)
    	{
    		pcCurrent->sayI();
    	}
    }
    I add objects like so:
    Code:
    Obj::listAdd(new Obj(1));
    And remove them like so:
    Code:
    //0 is the index, or distance from the start of the linked list.
    Obj::listRemove(0);

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, does adding them to your list in the first place leave a noticeable impact on memory?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    Quote Originally Posted by major_small
    well, does adding them to your list in the first place leave a noticeable impact on memory?
    Uh, no, but the memory usage isn't the issue here. Being able to successfully delete dynamically allocated memory is.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ...so then how exactly do you know that it's not getting deleted?

    if you can't see the memory chunk allocated, how do you expect to watch for it's deallocation?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    Quote Originally Posted by major_small
    ...so then how exactly do you know that it's not getting deleted?

    if you can't see the memory chunk allocated, how do you expect to watch for it's deallocation?
    I watched it in task manager. The memory usage for the program increased by 2KB when allocating 50 of them dynamically, and did not decrease when I deleted them.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Getting and freeing memory from the OS is expensive.

    So when you delete a block of memory, it returns to the pool which is free memory owned by the process.
    This makes future allocations a lot more efficient without involving the OS.

    Once you've got a certain amount from the OS, then a loop of new/delete calls should not cause the total amount allocated to the process (as seen by task manager) to increase over time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    Quote Originally Posted by Salem
    Getting and freeing memory from the OS is expensive.

    So when you delete a block of memory, it returns to the pool which is free memory owned by the process.
    This makes future allocations a lot more efficient without involving the OS.

    Once you've got a certain amount from the OS, then a loop of new/delete calls should not cause the total amount allocated to the process (as seen by task manager) to increase over time.
    So space for allocation is retained in the process after something is deleted?

    How do I give it back to the OS without ending the process? Other programs may need it.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > How do I give it back to the OS without ending the process?
    It's all virtual memory anyway - the total amount of virtual memory allocated is likely to be more than the amount of physical memory you have.

    If your program doesn't touch a block of memory for a while, then the OS is likely to move it to the swap file if it needs the underlying physical memory for something else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I use VC++ .net and my way to check if I deallocated memory correctly is to use something like this (a quick example):
    Code:
    #include<iostream>
    #include<crtdbg.h>
    using namespace std;
    
    int main()
    {
    	int * array = new int[10];
    	
    	_CrtDumpMemoryLeaks();
    	return 0;
    }
    And then, press F5 (debug) to see results. If you see something like this:
    Detected memory leaks!
    Dumping objects ->
    {49} normal block at 0x00372DB8, 40 bytes long.
    Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
    Object dump complete.
    The program '[1688] CppTestovi.exe: Native' has exited with code 0 (0x0).

    you have a memory leak.
    If you deallocated memory corectly the, the rest is up to your OS.

    To be honest I don't know any other quick way for checking about memory deallocation especially using other IDEs. Maybe somebody will give us good example.

    Cheers!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. calculate memory consumption and cpu usage
    By GermanDev in forum C++ Programming
    Replies: 18
    Last Post: 12-14-2008, 05:32 PM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM