David Heffernan 2345 Posted August 19, 2020 7 hours ago, Mahdi Safsafi said: Obviously soon or later, a system paging will work and starts to swap pages from memory to disk. Not obvious at all. Usually you allocated memory because you wanted to use it. If you've swapped the entire block to disk then bringing a couple of pages back is the least of your worries. Solve your problem by avoiding swapping in the first place. Nothing you have said, in my view, supports your claim that all allocation of huge blocks should be done using VirtualAlloc. 1 Share this post Link to post
Mahdi Safsafi 225 Posted August 19, 2020 7 hours ago, Stefan Glienke said: Yes some are written to disk but usually not commonly used memory - especially not when there is plenty of RAM available. Yes Stefan but that does not change the fact that FreeMemory can be a bottleneck. The example I showed demonstrates paging. While its the most one that may have a several impact, there are other players too (cache thrashing, tlb thrashing, ...). Share this post Link to post
Mahdi Safsafi 225 Posted August 19, 2020 1 hour ago, David Heffernan said: Not obvious at all. Usually you allocated memory because you wanted to use it. If you've swapped the entire block to disk then bringing a couple of pages back is the least of your worries. Solve your problem by avoiding swapping in the first place. Nothing you have said, in my view, supports your claim that all allocation of huge blocks should be done using VirtualAlloc. Far now, we only have your words ! Mention a study, benchmark or a formal statement that claims your words. With all what was pointed out about the difference between GetMemory/FreeMemory and VirtualAlloc/VirtualFree and you still think that GetMemory/FreeMemory is suitable for large data ... WOW ! Share this post Link to post
Stefan Glienke 2002 Posted August 19, 2020 As a library developer I have this question after reading all this: how does this affect me. For example collections are an essential part of spring4d and they can be of any capacity from just a few items which fit into a small block up to collections that hold thousands of elements and where SetLength causes the used GetMem to use large blocks. So if anyone claims that the way the MM does it is not good the solution for many places that allocate variable size of memory is not to put if size < x then getmem else virtualalloc but to solve this properly inside of GetMem/the memory manager. Especially since GetMem is indirectly called by many things such as SetLength - if you directly allocate memory for your own sure you can choose one or the other. Share this post Link to post
David Heffernan 2345 Posted August 19, 2020 3 hours ago, Mahdi Safsafi said: Mention a study, benchmark or a formal statement that claims your words. I don't need to. I'm not the one making the claim that all huge memory allocations should be made using VirtualAlloc. It's you that is making that claim. Share this post Link to post
Mahdi Safsafi 225 Posted August 19, 2020 2 hours ago, Stefan Glienke said: As a library developer I have this question after reading all this: how does this affect me. For example collections are an essential part of spring4d and they can be of any capacity from just a few items which fit into a small block up to collections that hold thousands of elements and where SetLength causes the used GetMem to use large blocks. So if anyone claims that the way the MM does it is not good the solution for many places that allocate variable size of memory is not to put if size < x then getmem else virtualalloc but to solve this properly inside of GetMem/the memory manager. Especially since GetMem is indirectly called by many things such as SetLength - if you directly allocate memory for your own sure you can choose one or the other. Yeah ! The real problem is not how to allocate the memory. But how to free it. In other word how efficiently someone will make difference about a pointer that belongs to a small block and a pointer from a large block without de-referencing it. I believe that understanding the drawbacks is really required. i.e: Someone can benefit form it by freeing a collection items in the reverse order. Just one question Stefan : Why not let the user specify the nature of the collection (small, large) ? Share this post Link to post
Guest Posted August 19, 2020 2 hours ago, Stefan Glienke said: how does this affect me. To write down the whole thing in short, Use MM everywhere unless yo know the that big memory allocation is needed and only if the speed is a concern then switch those big allocation to direct OS allocation, with Stefan Sping4D you would go with MM calls, as they required size will vary from very small to very big, with dummzeuch buffers it is better to switch to OS calls and manually manage them, because he knows the size and they will include IO operations. Mahdi example about page file involvement is brilliant, that behaviour is very visible with RAD IDE, when your IDE(s) was running for hours and suddenly you decided to open Chrome or FireFox, you will observe the flickering in IDE on switch , and if you used the IDE here then switching to Chrome you will also observe the flickering, now close one of them and everything will get better to some extend, this can happen with any of your PC's highly depends on the system memory, but RAD IDE manage to screw you system even when system has 50% free due the fragmentation, it does allocate thousands of those 4k that occupy 64k blocks and render them as waste, system will not report them as used, but as free! Now, when system need more memory it will look for the less accessed memory blocks and find those blocks of 64k and marshal them to swap file, system doesn't marshal small blocks like 64k but many of them in one operation( not really one operation but step-step operation watch the resource monitor to get the idea). that the core reason why RAD IDE has this very ugly behaviour as it rubs the system the worst way. Share this post Link to post
Mahdi Safsafi 225 Posted August 19, 2020 49 minutes ago, David Heffernan said: I don't need to. I'm not the one making the claim that all huge memory allocations should be made using VirtualAlloc. It's you that is making that claim. Its not like you don't need to ... the fact there is none ! Share this post Link to post
Lars Fosdal 1792 Posted August 19, 2020 @Kas Ob. My last three laptops have had 32Gbs of RAM. Page file involvement is pretty rare. Share this post Link to post
Guest Posted August 19, 2020 11 minutes ago, Lars Fosdal said: My last three laptops have had 32Gbs of RAM. Page file involvement is pretty rare. Sorry i am missing the point here, is it one of these 1) You have no problem in developing software, then does all your software run on 32gb? 2) You don't see any problem with the IDE or Chrome and the flicking or performance degradation ? If you would, please explain what are you correcting me on. Share this post Link to post
Lars Fosdal 1792 Posted August 19, 2020 If your software has issues with page file involvement - your machines are not up to spec. Share this post Link to post
Mahdi Safsafi 225 Posted August 19, 2020 29 minutes ago, Lars Fosdal said: @Kas Ob. My last three laptops have had 32Gbs of RAM. Page file involvement is pretty rare. The paging is not the only player. The guys were running on a large RAM(128GB) and suffered from a potential degradation of performance : see Thrashing Share this post Link to post
Guest Posted August 19, 2020 So, according to Microsoft my PC is up to spec, according to Google my device is up to spec, but when i run an IDE from Embarcadero i should through more 16gb to be up to spec, even the system is reporting 65% free memory. My PC is not swapping even when i have one Hyper-V guest running along Visual Studio and between 3-20 tabs of Chrome along with few more mini tools like Process Explorer, FireSeek, Beyond Compare and resource monitor for hours, all goes south once i open one IDE and build an app 3 times, now i have to close half of those application or even restart the PC. One more thing : you are saying that my PC is not up to spec, then yours too, the difference is your system with start show my PC symptoms may be in double or triple the time. Share this post Link to post
Lars Fosdal 1792 Posted August 19, 2020 "suffered from potential degradation" ? Either you suffer, or it is entirely potential. "The guys" ? Share this post Link to post
Guest Posted August 19, 2020 16 minutes ago, Lars Fosdal said: If your software has issues with page file involvement Sorry Lars, here i need clarification too, by "your software" you meant the software i build or the software i use/run ? Share this post Link to post
Mahdi Safsafi 225 Posted August 19, 2020 5 minutes ago, Lars Fosdal said: "suffered from potential degradation" ? Either you suffer, or it is entirely potential. I really don't know what you mean. Quote "The guys" ? https://raima.com/memory-management-allocation/ Share this post Link to post
Stefan Glienke 2002 Posted August 19, 2020 1 hour ago, Mahdi Safsafi said: Just one question Stefan : Why not let the user specify the nature of the collection (small, large) ? They do by possibly specifying a capacity when creating one. Should I depending on that number either call SetLength which uses GetMem or allocate the dynamic array memory buffer myself? I would rather like to avoid that because that might get quite complicated when we are talking about resizing. 1 hour ago, Kas Ob. said: you would go with MM calls, as they required size will vary from very small to very big That is what I was aiming at - making claims of how x is bad and y is better is all good and sound but what are the concequences for developers that want to benefit from these potential benefits - or does it not matter at all. Share this post Link to post
Lars Fosdal 1792 Posted August 19, 2020 @Kas Ob. - The 32-bit IDE is running on WoW. My IDE crashes too. Out of memory or it simply hangs in an internal deadlock. Several times per day. But - it has never crashed or trashed my Windows 10 Enterprise 1909 system. My uptime is typically about a month, i.e. until the next security patches. RAD Studio, Visual Studio, VS Code, PowerShell 7, MS SQL Server Manager, MS SQL Server, a large number of remote desktops, Excel, Outlook, OneNote, Teams, Discord, Chrome with too many tabs, Apex SQL Diff, Beyond Compare, VirtualBox, and the invaluable Process Hacker. I was thinking of the software you write - if it has page file issues - you either have HW spec issues, or you have allocation strategy issues. 1 Share this post Link to post
Arnaud Bouchez 407 Posted August 20, 2020 (edited) On 8/19/2020 at 2:03 AM, Mahdi Safsafi said: Now, if you just used VirtualAlloc/VirtualFree, de-referencing the pointer is not required and all steps from 1 to 6 aren't necessary at all and paging is not happening too !!! The important thing is that some of the above steps are heavy ... and that's why on their analyze they were taking hours to free the memory. Because a swap from/to disk/memory is happening all the time. If the memory pages are swapped on disk, then indeed it will be slow to dereference the pointer. But in this case, the application is very badly designed: paging on disk should be avoided in all cases, and direct disk API calls should be made instead to flush the data. The problem is not the use of the MM. The problem is the whole memory allocation design in the application. Less memory should be allocated. This is what @David Heffernan wrote, and you didn't get his point about swapping. If the memory page is not on disk - then you may have a cache miss when the pointer is dereferenced. For big memory blocks, it won't hurt. Calling VirtualFree will take definitively much more CPU than a cache miss. So I still don't find the relevance of your argumentation. Last but not least, the article you quoted (without any benchmark and code to prove the point) is very specific to the memory use of a database engine, which claims to be the fastest on the embedded market. I doubt everytime I read such claim, and I don't see actual code. More like technical marketing arguments than real data. Raima DB features "needing 350KB of RAM" and "optimized to run on resource-constrained IoT edge devices that require real-time response". So what is the point of benchmarking handling of GB of RAM? The whole https://raima.com/sqlite-vs-rdm/ is full of FUD. The graphs are a joke. Since they don't even show the benchmark code, I guess they didn't even use a fair comparison and use SQLite in default mode - whereas with exclusive mode and in-memory journal, SQLite3 can be really fast. We have benchmark and code to show that with mORMot - https://synopse.info/files/html/Synopse mORMot Framework SAD 1.18.html#TITL_60 and https://synopse.info/files/html/Synopse mORMot Framework SAD 1.18.html#TITLE_140 (current numbers are even higher). You may have to find better references. Edited August 20, 2020 by Arnaud Bouchez 4 Share this post Link to post
Mahdi Safsafi 225 Posted August 20, 2020 (edited) @Arnaud Bouchez @David Heffernan EDIT: We reached a far point and I believe that we're not going to agree. So lets make an end to this argument. Edited August 20, 2020 by Mahdi Safsafi Share this post Link to post
Mahdi Safsafi 225 Posted August 20, 2020 Mostly forgot 20 hours ago, Kas Ob. said: Mahdi example about page file involvement is brilliant. You're wrong ! its not the example that is brilliant. its people that understand it. You're the brilliant. Share this post Link to post
Daniel 417 Posted August 20, 2020 50 minutes ago, Mahdi Safsafi said: [...] Peace ! ... well - if this is really what you want, you should rethink your discussion-style. For me your last post is quite offensive. It is not the first time we (have to) talk about this. 4 Share this post Link to post
Mahdi Safsafi 225 Posted August 20, 2020 @Arnaud Bouchez @David Heffernan I'm really sorry guys. I didn't meant to be offensive by any way. I just got some external things that subjectively influenced the way I express. @Daniel I just discarded my last post. 1 Share this post Link to post