Archived

This topic is now archived and is closed to further replies.

kroiz

why release memmory?

Recommended Posts

hi i use windows 2000. and program in c/c++. i thought that when i run a program it gets it''s own memmory space. and when the program terminates that memory space is released. so why release the memmory i alloc? didn''t ment to be provocative. just curious plz be gentle. 0 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
quote:
Original post by kroiz
hi
i use windows 2000. and program in c/c++.
i thought that when i run a program it gets it''s own memmory space.
and when the program terminates that memory space is released.
so why release the memmory i alloc?
didn''t ment to be provocative. just curious plz be gentle.

0 error(s), 0 warning(s)


well, my understanding (might not be correct) is that when it runs, it grabs a chunk of free memory. now if you wasnt to release that memory (so it can be re-used) then the comp still thinks its being used.... after the prog is closed. now eventually, if you keep running the program and not releasing the memory the comp will run out of memory and you will need to reboot your machine.

by not releasing the memory, it would be called a "memory leak"

Share this post


Link to post
Share on other sites
Basically, your computer has a limited amount of memory. When you allocate memory, the OS gives you a little more of the currently unused memory. If your program allocates memory in chunks of 1k (say) and you have 23Mb of free RAM, then it''ll take about 23552 allocations for your computer to run out of memory.

When your computer has run out of physical memory, it starts ''paging'' unused memory to disk, to free up physical memory. When a program wants to use some memory that has been paged to disk, Windows must pause the program until the memory is available. On Win2k you''re no too bad, but 95-based systems (95/98/ME) suck at multitasking, particularly whilst storage devices are being accessed (try running a CPU intensive program whilst accessing the floppy drive, and you''ll know what I mean). The upshot is, that whilst Windows is saving the memory of other applications to disk to free up space in physical memory, all the programs grind to a halt.

You can help solve this by freeing memory as soon as it is no longer needed. This means that it''ll take longer for Windows to run out of memory, which means it''ll take longer for Windows to need to start paging.

As well as paging, Windows will move applications around in memory to make managing them easier. Windows calls this compacting, and when Windows determines that it is spending a lot of time compacting it sends the WM_COMPACTING message to all top-level windows, to ask them to free some memory. This makes those programs smaller, which makes it easier for Windows to move them about, and find memory to allocate to them.

''Nuff said. I''ll enjoy watching you live, demon.

Share this post


Link to post
Share on other sites
quote:
Original post by Bezzant
well, my understanding (might not be correct) is that when it runs, it grabs a chunk of free memory. now if you wasnt to release that memory (so it can be re-used) then the comp still thinks its being used.... after the prog is closed. now eventually, if you keep running the program and not releasing the memory the comp will run out of memory and you will need to reboot your machine.



Yes and no. Although physical memory proper won''t be ''lost'' if your program quits without freeing it, Windows also has a different kind of thing that is allocated, known as resources. A resource can be a pen or a brush for drawing, a font, a region (which defines a region on a device context), a device context (which, as you may know, is a object through which you can draw to a window), windows themselves, and sundry other things.

On NT-based systems (including Win2k), Windows is good with resources. Each program gets it''s own batch, and they are freed when the program quits.

On 95-based systems, Windows is bad with resources. Each program is allocated resources from a system-wide pool of handles. Windows will usually free these resources when your program quits, but sometimes it doesn''t. Pens, brushes and fonts, in particular, will not always be freed, but windows and dcs will. If your application uses pens, brushes and fonts without freeing them before it closes, then your system will run out of resources. On Win2k you won''t see this, buit if anyone tries to run it on a 95-based system, they will.

''Nuff said. I''ll enjoy watching you live, demon.

Share this post


Link to post
Share on other sites
Want a job in industry?

Start a habbit of releaseing memory when you are done with it. Not all programs run for 5 minutes and then are closed. I write servers that require continual operation over a period of months. Memory leaks have a nasty habbit of accumulating and bogging down servers.

Not releasing memory is viewed at best as sloppy code and at worst as just plain lazy.

Yes the OS releases memory when the app closes down, but what are you NOT going to do next because the OS handles it? Stop closing files? Not bothering to release resources? There are other apps and services running on the OS that may need to use these resources at some point.

I have worked with people in the industry who have this attitude, they don''t get far and are regaurded as poor programmers. Odd though, they regaurd themselves as top notch, which is a joke because none of thier stuff will run for more than a couple of days. Why? Irresponsible resource management.

So do yourself a BIG favor and increase the chances of a good industry job. Release memory.


D.V.

Share this post


Link to post
Share on other sites
Ok thank you all.
i got it now.
actully i do always release memory. just don''t like doing stuff i don''t fully understand it reasons.
:-)
BTW DeltaVee i am employed in the industry even without knowing why i should release
memory. ;-)

0 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
Lots of people are

And from time to time I am called up at 3am to drive 30 miles in to bounce a server because it has no memory left. But what do I care, I am a contractor and I bill double time


D.V.

Share this post


Link to post
Share on other sites
We have one server that is so bad I need to put it on my shoulder and walk it up and down the corridor and pat its back.

But that is what I would expect in an NT nursery

D.V.


Edited by - DeltaVee on October 30, 2001 10:26:07 AM

Share this post


Link to post
Share on other sites
I have a question.

I know that when you create pointers that you always
should initialize them to NULL. And i also always set them to NULL when im done with them, however i dont now the reason why
i should do this. I know that pointers can have any kind of
value when it is created and thats why you set them to NULL,
but why do i need to set them to NULL when i''m done with them?

Share this post


Link to post
Share on other sites
Calling delete on a NULL pointer is guaranteed to fail gracefully, eg do nothing at all. If you call delete on a pointer, fail to set it to NULL, and try to delete the same pointer once more, lots of bad and mysterious things can and will happen.
Personally I believe garbage collection is the future for all but the most time critical programs. I''m also getting a little tired of that silly l33ter-than-thou-because-I-manage-my-own-memory attitude.

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

Share this post


Link to post
Share on other sites
Another reason to relase the memory is so that you can use the same physical memory again and again, instead of always asking for more when you need it.

Share this post


Link to post
Share on other sites