memory leaks
#1 Members - Reputation: 100
Posted 09 August 2010 - 10:38 PM
The reason I ask is because I have been thinking alot about performance lately and how my windows 7 is about 3 times more responsive and less resource hungry than my vista box, how my chrome is 10x faster than my firefox and does not experience the same leaks.
Also I have noticed that contrary to popular belief, PR etc, most open-source software performs slower than non-oss, so for example ie8 is faster at loading webpages, flash etc than firefox 3.6x
The main exception seems to be Linux OS which is in some ways faster and other ways slower than Windows OS while the two have I would say after a standard install of a packages linux about 10% either way in functionality
#2 Members - Reputation: 2047
Posted 09 August 2010 - 11:13 PM
Quote:That is the reason why memory leaks usually don't happen with stack-based allocations (although it is technically possible to leak stack memory, e.g. with alloca inside a loop).
as far as I knew the stack would be added to and as soon as a variable went out of scope it would be destroyed
Most memory leaks will come from heap allocations, either because of exceptions or simply because of programmer sloppiness. Wrapping heap allocations/deallocations into stack-allocated objects (in the simplest case, that can be std::auto_ptr) is a well-known remedy for that. Keyword: RAII.
Using the non-throwing swap idiom is another example remedy.
#3 Members - Reputation: 122
Posted 09 August 2010 - 11:18 PM
Why memory leaks occur. Here's the deal. When you use malloc() or new, you're asking the kernel for a chunk of memory. Your pointers on the stack are "dumb pointers". They do not know what to do when they go out of scope, so the memory is not freed. This is why you must call free() or delete respectively in order to let the kernel know you are done with that memory, so it can use it somewhere else. Languages with garbage collection, like Java, may use reference counting. This basically keeps references of how many instances of your pointer exist, and deletes the pointer when that number reaches 0. I personally use my own shared pointer class, but i know Boost provides one as well.
with your FOSS vs non-FOSS argument, it really depends on the application domain. Some projects are handled better than others, and thus outperform their proprietary counterparts. Some FOSS projects are just not as lucky. The problem is not whether or not it's FOSS, but whether or not the project is managed appropriately.
I hope this helps you out.
#4 Members - Reputation: 491
Posted 18 January 2011 - 12:23 AM
If you're really interested in memory management, read up on and learn to use smart pointers (the concept and a library that implements them) thoroughly before starting your project. I found Boost libraries after having already started a game and went back through to insert them. Was something of a nightmare.
I Create Games to Help Tell Stories | Writing Blog
#5 Moderators - Reputation: 14296
Posted 18 January 2011 - 12:40 AM
You allocate memory but never release the allocation.How & why do they happen
Easy answer: Don't use new or malloc.and how can programmers avoid them. I use c++ because I love it XD, how can I stop any memory leaks
Harder answer: Make sure every new/malloc is paired up with a delete/free.
C++ answer: Use smart pointers / RAII
Leaks tend to be heap allocations (new/malloc), not stack allocations.as far as I knew the stack would be added to and as soon as a variable went out of scope it would be destroyed
I don't see the connection between your anecdotes and the question of memory leaks...windows 7 is about 3 times more responsive and less resource hungry than vista ... chrome is 10x faster than firefox ... most open-source software performs slower than non-oss ... ie8 is faster than firefox 3.6x ... Linux OS is in some ways faster and other ways slower than Windows OS ...
#6 Members - Reputation: 171
Posted 18 January 2011 - 11:14 PM
For java, we have FindBug. As to C/C++, MemCheck is an popular tool. All these tools are free to use and are able to find your memory leaks in programs.
#7 Moderators - Reputation: 5304
Posted 19 January 2011 - 06:01 AM
The reason I ask is because I have been thinking alot about performance lately and how my windows 7 is about 3 times more responsive and less resource hungry than my vista box, how my chrome is 10x faster than my firefox and does not experience the same leaks.
It all depends. Is a cache a leak? Maybe it should be considered that way, if its hit rate is low and size is large, or maybe it can be tuned better. In most of the examples you gave, I suspect that improvements in the caching algorithms were big contributors to the performance differences between them.






