BTW, while using the PB debug output, memory consumption may grow slowly, too.
It is the strings that get added to the debug output window.
Better disable debugger and write info to a log file, if you want to make sure nothing disturbs you.
any tip/trick for avoid memory leak?
-
sec
- Enthusiast

- Posts: 792
- Joined: Sat Aug 09, 2003 3:13 am
- Location: 90-61-92 // EU or ASIA
- Contact:
Re: any tip/trick for avoid memory leak?
Thanks you for great tips/tricks.
@Danilo: EmptyWorkingSet_() , HeapCompact_() solve the problem
@Danilo: EmptyWorkingSet_() , HeapCompact_() solve the problem
Re: any tip/trick for avoid memory leak?
I recommend to run your program (or test procedure) for some hours (many iterations),sec wrote:@Danilo: EmptyWorkingSet_() , HeapCompact_() solve the problem
using EmptyWorkingSet_() and HeapCompact_() in the test/debug build before logging
memory consumption. 1 call to EmptyWorkingSet_() and HeapCompact_() shows results
immediately, but you need to make sure memory consumption is stable in the long run.
I used this when searching for memory leak and found the p-ascii leak bug (already fixed).
If handles and memory consumption is stable after many million iterations using EmptyWorkingSet_() and HeapCompact_(),
it is no leak.
-
IdeasVacuum
- Always Here

- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: any tip/trick for avoid memory leak?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: any tip/trick for avoid memory leak?
Just don't leave them permanently in your program, they don't solve the problem if there is really one and interfere with the more efficient mechanism inside the OS to do packing/cleaning by itself when really needed.sec wrote: @Danilo: EmptyWorkingSet_() , HeapCompact_() solve the problem
They can help, as Danilo said, to see more easily if leaking is occurring since they are forcing to OS to reduce the working set of the process to the minimum and (maybe) compact the heap memory. I have some doubts about the usefulness of HeapCompact though on a OS not configured for debugging.
"Have you tried turning it off and on again ?"
-
IdeasVacuum
- Always Here

- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: any tip/trick for avoid memory leak?
....Windows is very good, but not always as efficient as it could be, which is why there is a market for apps like Cacheman.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: any tip/trick for avoid memory leak?
I know there are a lot of people who like Cacheman a lot, I find Cacheman pretty useless.
There is some freeware which can do the little that can be done and that can in certain special cases make sense.
For the rest I really think buying a stick or RAM is a much better investment, working on every platform and every OS.
A pearl from Cacheman website -> http://www.outertech.com/en/how-to-spee ... r-computer
On that page you can read:
There is some freeware which can do the little that can be done and that can in certain special cases make sense.
For the rest I really think buying a stick or RAM is a much better investment, working on every platform and every OS.
A pearl from Cacheman website -> http://www.outertech.com/en/how-to-spee ... r-computer
The linked page where Microsoft "recommend" Cacheman is this -> http://support.microsoft.com/kb/835645/en-ussomeone wrote: Even Microsoft recommended Cacheman 5.11: "Cacheman enables you to tweak the cache settings for your hard drive, CD-ROM drive, desktop icons and file and folder names in much more detail. You can pick a typical profile, use the wizard or just experiment."
On that page you can read:
So those words were from an article from a magazine, talking about Windows memory management for Windows 98.Microsoft wrote: The articles set out below are articles created and/or produced by Future Publishing Limited. Microsoft is not responsible for the content, accuracy or opinions expressed in these articles.
"Have you tried turning it off and on again ?"
Re: any tip/trick for avoid memory leak?
Also keep in mind that Windows caches stuff. So even if you free memory then Windows may still keep that memory in your program just in case your program will use it again (thus task manager may show higher memory use than you actually have).
The same happens if you read files, Windows will have files in it's filecache even after you are done and have closed the files, this saves time in case your program or another program need to read the file again.
If system memory starts to get low, then you will find that Windows will magically start to "free" the memory of the system and programs.
Reading from disk, or re-allocating memory is costly, just leaving it in memory for as long as possible saves energy and time.
So even if a system with 8GB has only 2GB memory free, of those 6GB in use maybe as much as half could previously "freed" memory or filecache or other cached stuff.
Also not the following.
When you close/quit your program, make sure to read the API documentation (if you use any API stuff) and close any handles that the documentation says that you "must" close, this is to avoid handle leaks.
However, any memory allocated by your threads and main thread/process, does not need to be freed.
Instead of looping through and freeing everything or closing all windows (you may need to free custom window properties you added, see API documentaton) and things like that,
just quit, windows will automatically free all normal memory and handles (PureBasic also cleans up a few things for you).
So if you Allocate some memory for some data processing at the start of your program, then you do not need to free it at the end, just quit, freeing or Cleaning memory eats up time and CPU. Ever seen a "big" program that you quit take ages to actually quit (look at it in taskmanager) it is most likely freeing all the memory it allocated, which is not really needed.
Another thing that many forget is that it's better to allocate memory once only.
By this I mean if you know you will need x amount of memory, then allocate that memory at the start of your program, that way you save time and CPU as you do not need to allocate and free, allocate and free in your loop all the time.
Memory leaks is also less likely to happen as you can more easily see what memory you have allocated.
Not sure if there is a coder term for this, but you can call it pre-allocation of memory. (or static allocations?)
And Luis is right, memory/cache tools like that do not help much, maybe in the days of Windows 98 (which is the OS mentioned there on the MS page).
All you as a programmer (and user) need to know is that the memory use shown in taskmanager does not reflect reality.
Windows will hold on to memory that a program has freed for a long time, it may be marked as "freeable" but not actually freed yet (for other programs to use).
If memory runs low then Windows will start to actually free it/make it available to the active programs.
If you quit a program the memory the program used may still not be fully freed, the OS may just hold on to it, in case you start the program again, that way it avoids having to load the exe or dll or file from disk again.
Because of all this under the hood magic, all those "memory freer" tools you see all over the net is just nonesense, if you are lucky they do not chance anything at all, at worst they will reduce the OS performance as the program will actually be fighting against the caching of Windows. Some of these tools also free memory by allocating ALL memory and freeing it again, thereby kicking in the OS low memory routines, which nukes a lot of the caches and worse, may cause a lot of running programs to be fully or partially swapped to the pagefile, hurting performance.
Right now my system uses 2.5GB of 8GB total physical memory, this is around 30%, which is typical for my system. I've got a ton of free memory. Also, I'm sure tat half of the memory in use is just windows caching programs and files or memory marked as "free" but not yet actually freed.
Note: I took a peek at cacheman features, it actually tweaks the system cache behaviour, which means this is not the same as all those memory freer tools out there. Cacheman might help you tune your OS caching to your needs if you are careful and know what you are doing.
Windows Vista and later has very smart caching, but sometimes it might guess wrong or take a longer time to learn your software habits than you'd like to wait.
The same happens if you read files, Windows will have files in it's filecache even after you are done and have closed the files, this saves time in case your program or another program need to read the file again.
If system memory starts to get low, then you will find that Windows will magically start to "free" the memory of the system and programs.
Reading from disk, or re-allocating memory is costly, just leaving it in memory for as long as possible saves energy and time.
So even if a system with 8GB has only 2GB memory free, of those 6GB in use maybe as much as half could previously "freed" memory or filecache or other cached stuff.
Also not the following.
When you close/quit your program, make sure to read the API documentation (if you use any API stuff) and close any handles that the documentation says that you "must" close, this is to avoid handle leaks.
However, any memory allocated by your threads and main thread/process, does not need to be freed.
Instead of looping through and freeing everything or closing all windows (you may need to free custom window properties you added, see API documentaton) and things like that,
just quit, windows will automatically free all normal memory and handles (PureBasic also cleans up a few things for you).
So if you Allocate some memory for some data processing at the start of your program, then you do not need to free it at the end, just quit, freeing or Cleaning memory eats up time and CPU. Ever seen a "big" program that you quit take ages to actually quit (look at it in taskmanager) it is most likely freeing all the memory it allocated, which is not really needed.
Another thing that many forget is that it's better to allocate memory once only.
By this I mean if you know you will need x amount of memory, then allocate that memory at the start of your program, that way you save time and CPU as you do not need to allocate and free, allocate and free in your loop all the time.
Memory leaks is also less likely to happen as you can more easily see what memory you have allocated.
Not sure if there is a coder term for this, but you can call it pre-allocation of memory. (or static allocations?)
And Luis is right, memory/cache tools like that do not help much, maybe in the days of Windows 98 (which is the OS mentioned there on the MS page).
All you as a programmer (and user) need to know is that the memory use shown in taskmanager does not reflect reality.
Windows will hold on to memory that a program has freed for a long time, it may be marked as "freeable" but not actually freed yet (for other programs to use).
If memory runs low then Windows will start to actually free it/make it available to the active programs.
If you quit a program the memory the program used may still not be fully freed, the OS may just hold on to it, in case you start the program again, that way it avoids having to load the exe or dll or file from disk again.
Because of all this under the hood magic, all those "memory freer" tools you see all over the net is just nonesense, if you are lucky they do not chance anything at all, at worst they will reduce the OS performance as the program will actually be fighting against the caching of Windows. Some of these tools also free memory by allocating ALL memory and freeing it again, thereby kicking in the OS low memory routines, which nukes a lot of the caches and worse, may cause a lot of running programs to be fully or partially swapped to the pagefile, hurting performance.
Right now my system uses 2.5GB of 8GB total physical memory, this is around 30%, which is typical for my system. I've got a ton of free memory. Also, I'm sure tat half of the memory in use is just windows caching programs and files or memory marked as "free" but not yet actually freed.
Note: I took a peek at cacheman features, it actually tweaks the system cache behaviour, which means this is not the same as all those memory freer tools out there. Cacheman might help you tune your OS caching to your needs if you are careful and know what you are doing.
Windows Vista and later has very smart caching, but sometimes it might guess wrong or take a longer time to learn your software habits than you'd like to wait.
Re: any tip/trick for avoid memory leak?
I have tried it for a while, because originally all it did was to manage the windows cache (now they added some other things more or less useful) and that put it apart from more "scam-only" programs that are now long forgotten which were doing the most stupid things (not last the massive allocation "trick" you mentioned). Also because a lot of people were/are so enthusiast about it.Rescator wrote: Note: I took a peek at cacheman features
Initially I had a general opinion based on the assumptions about what these programs can do, and that was confirmed even in this case trying it on the field later. I didn't find any real use for it on a modern OS with a decent HW configuration. I didn't like the "microsoft recommend" stunt either.
This is free -> http://www.pcwintech.com/about-cleanmem (was free, now the GUI seems to be 5$, but the core program is still free)
This MAY be useful if you have a system with not much RAM, a really slow disk used for paging, and depending on the usage pattern of the programs you run.
It uses the API discussed in this thread, EmptyWorkingSet() to try to avoid as much as possible to write to the swap file.
If you have enough RAM you can forget this one too.
Also see -> http://technet.microsoft.com/en-us/sysi ... 97561.aspx
"Have you tried turning it off and on again ?"
-
IdeasVacuum
- Always Here

- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: any tip/trick for avoid memory leak?
That reference to Microsoft by Outertech is bad. I have written to them about it, pointing out that the article only mentions Cacheman on the very last line: "Experiment with different settings to see what works best for you with Cacheman (www.outertech.com)." and that there is no contribution by Microsoft at all. A response will be interesting. If they were a UK based company they would be looking at a heavy fine for misleading advertising - being based in Germany, they could find themselves in trouble for that issue, I don't know. Their quote from PC magazine goes back to 2002, which is a little bit cheeky given that Windows has changed a bit since then!
In their defence, Cacheman does seem to work (it is a good Task Manager, if nothing else). Of course, the benefits may only be psychological....... I ran it for some time, over a year, but stopped using it because the system tray display info was unreliable, requiring a re-install to fix it. My thoughts were, you don't want a buggy app messing around with your RAM. That could be just my PC of course.
In their defence, Cacheman does seem to work (it is a good Task Manager, if nothing else). Of course, the benefits may only be psychological....... I ran it for some time, over a year, but stopped using it because the system tray display info was unreliable, requiring a re-install to fix it. My thoughts were, you don't want a buggy app messing around with your RAM. That could be just my PC of course.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
