Yaron 53 Posted September 1, 2021 I'm using Delphi 7, 16GB ram, Windows 10. I needed to write a SoundFX playback module that supported multiple audio devices, volume level, and multiple effects playing. I thought the smartest/cleanest way would be to create a new thread for each SFX, play it using DirectShow and then let the thread clean itself up. It seemed like an elegant solution, just fire and forget, windows took care of the sound mixing and it worked reasonably well. That is, until my GDI+ based JPEG decoding code started exhibiting random memory allocation issues. I'm not too familiar with underlying windows memory management, but it seems that there is memory defrag operation that works in the background, otherwise the memory allocation issues would be constant and not random (working, not working and working again in the span of a second). Is there a WinAPI function I can call to defrag my app's memory? In the meanwhile I'm trying to work around this issue by reusing threads instead of going through many create/destroy cycles, but I'm still interested if there's an answer to my defrag question. Share this post Link to post
FPiette 383 Posted September 1, 2021 21 minutes ago, Yaron said: Is there a WinAPI function I can call to defrag my app's memory? There is not that I'm aware of. And I think it is not possible. Defragmenting memory would mean moving memory around and thus invalidate any pointer your application has. Also note that at the lowest level, there is virtual memory that the system use to build the virtual address space your application see. The virtual memory is managed by pages (Usually 4KB each). Those pages are mapped to the user address space to make a continuous block of virtual memory but the pages in physical memory can and are moved by the OS. The pages are even swapped to disk when physical memory comes low. Have a look at this wikipedia article about virtual memory. Share this post Link to post
Guest Posted September 1, 2021 1 hour ago, Yaron said: I'm trying to work around this issue by reusing threads instead of going through many create/destroy cycles, Recycle and cache are the keywords, meaning to design your application to work without freeing anything, reuse everything specially GDI+ stuff, because they really do nasty thing, and the popular method ( examples over the internet ) are to create another and free the old, track you objects and build a container for them, make such container responsible for creating, and freeing, where it will only free under conditions that you can tweak based on your observations and measurements, A thing to try with such design lets say you application need to decompress a JPG image, then you load the file then ask the the container (cache holder, pool,.. you name it) for a TJPGxx instance providing its size, and the container will reply with such close enough in size, after using it and decompress o whatever, don't clear it ! just return it to the container, same can be for all and every TBitmap you are going to use, by not clearing it and you can draw on it without resizing it, because resizing a bitmap can be a cause for memory defragmentation, it involve reallocation and data copy. I thing you got the idea as i have no idea what SoundFX is !, and don't know what GDI+ operations your application is running, again recycle and try not to free, (or clear) Share this post Link to post
Yaron 53 Posted September 1, 2021 @Kas Ob. For clarity, Sound FX is ".wav" file playback and I only use GDI+ to decode JPEG files. Share this post Link to post
Guest Posted September 1, 2021 5 minutes ago, Yaron said: Sound FX is ".wav" file playback and I only use GDI+ to decode JPEG files. Well, Sounf FX is still not clear to me But anyway, if all what you need is to play sound then check http://lakeofsoft.com/vc/ ( repeated answer, how much time should i do that !!!!! 🙂 ) , joking aside it has many many feature and really power library, one component in particular i think will interest you, the mixer, the mixer can combine multiple wav (audio) streams, mix them and then play them as one stream, check it and play with the demos and i hope you will solve your memory problem, not just hope i think you can solve you memory problem by well choosing better tools and approaches, as for JPEG decoding, well , GDI+ is not the best you to check better approaches and less memory intensive (specially with background threads), start with synopse https://synopse.info/forum/viewforum.php?id=5 Share this post Link to post
Vincent Parrett 750 Posted September 1, 2021 If you are using Delphi 7 with the default memory manager then you should look at an alternative memory manager like FastMM or NexusMM which do a much better job of reducing memory fragmentation. Share this post Link to post
Yaron 53 Posted September 1, 2021 @Vincent Parrett I'm using the latest version of FastMM Share this post Link to post