Jump to content


Popular Content

Showing content with the highest reputation on 12/26/20 in all areas

  1. Mike Torrettinni

    Example of wasteful, innefficient string manipulation

    Thank you @Kas Ob. I think now I understand my question was really poorly formulated, I was focused on strings manipulation, everybody else thought I was stress testing MM. Makes sense that some of you wanted me to go back to school 🙂 I thought I was becoming better at asking questions, I guess I need more work. Even though it probably seemed like a waste of time for everybody else, I learned valuable things during preparing the example, thinking about it, reading about FastMM and 'discussing' in this topic: 1. I still need to work on improving question quality 2. I'm probably little rebellious when getting suggestions like 'google this and then we can talk' 😉 3. What I thought was very wasteful, bad example of string manipulation, was actually not! 😉 even Delphi 7 handled it really well. If I tested this example in D7 before posting, I would probably realize this is not worth even asking. But it led me to test D7 from references to FastMM - to test version without it. So, something that is probably very basic and very obvious fact for seasoned developers, it wasn't for me and wouldn't come to this conclusion without this topic,
  2. Rollo62

    Chess board

    https://www.delphipraxis.net/141566-schach-ki-gesucht.html Some great links in the German DP recently.
  3. Kas Ob.

    Example of wasteful, innefficient string manipulation

    @Mike Torrettinni It is Ok and you are doing great. Let me explain a thing here, and will give you an example, the other day i suggested a thing for Mahdi, he liked it and mentioned it will be tricky with DLL's, now i went on explaining it is easy ! and he went explain to me how it is not easy, see here we both were right, and both failed to explain the context of our point of view, for me i was strictly pointing to PPointer usage with DLL (per one module) for patching for this specific new feature, while he was thinking about expanding DDetours itself for runtime hooking, thus we failed to convoy the context as both we assumed the other is seeing and thinking the same way, now if you tried imagine how others read our awkward conversation, some will assume Mahdi was right and i am wrong, some will assume i was pointing to the right thing while Mahdi is overengineering it, and most will not give a jack and see us as wasted space on the internet, the point is no matter how good social skills you have we always will fail at some point with our limited focus and our own unsupervised bias. And that why i suggested a book, a full book to read, and statistically speaking you will like it 😉 , and it will change you way of thinking, not only as programmer but as human being. ps: it still very useful exercise if you tried my example above in real program, so if you got time try to make your leak 1gb with only 1000 string, the experience from doing is valuable, and you will get to know how to detect such leaks or dangling memory, the one that will not reported as a leak because it is not.
  4. Stano

    ways to organize related code in a form?

    Because these people have no mantineles in their heads. Their mind / thinking is unlimited. This is the most beautiful thing for children. That's why I love them very much All of them! He didn't know it was impossible, so he did it. If you want to achieve something, you have to leave your truth!
  5. Kas Ob.

    Example of wasteful, innefficient string manipulation

    @Mike Torrettinni It is hard to explain something when 1) The point and the question to explain is not clear. 2) We don't know what is your assumptions are, like do you have assumptions we consider as basic and simple information and hard to miss, this will throw the conversation in awkward way, we will skip explaining something so simple by our consideration and you simply didn't know it, you will feel the question is not answered, and we continue to scratch our head. My suggestion is, please try to formulate the question in different way specially when you feel the point wasn't received clearly, and write what you think. Now to the way you are assuming fragmentation work, it will not happen if you just allocated x item and freed them all, to get fragmented memory you should allocate x and free y from them (where y<x) while keep the rest there and repeat without freeing them all, Delphi MM will allocate big chunk then serve it as small chunks to your application, when application ask for more the MM will allocate another big allocation and so on, on freeing: when big allocation is completely unused, mean you application return all the small allocation to MM, here most likely MM will return the big allocation to the OS, How to fragment this and stress the system, an example on how you can cause cause 1gb of memory to be reserved with only 1000 of very short string (1 char), Steps to do this : allocate many short string or arrays, let try explain this with arrays (easier), one 1 byte array like TBytes, the memory manager will allocate the smallest allocation for it it will need at least 1 byte+ 4 length = 5 bytes, the MM will allocate the smallest allocation and let assume it is 8, and here i will discard the look up table for it size (while there is at least 4), now to utilize 1gb we need 1gb/8b array, this size will require MM to allocate block with 1mb, so to keep 1mb indefinitely allocated you need just to leak one array per 1mb allocation ! try this on your own and allocate many strings or arrays and free all except 1 every X, find X on your own to cause 1000 unfreed string the use 1gb. I hope this explanation give you a hint on how to stress the OS by miss using MM (or abusing the MM) for string or anything else, and by the way this is very similar on why the IDE is ever growing in memory usage, not a real leak but dangling pointers with may be very few wrongly managed interfaces, because each compile it will perform very similar behaviour with many small allocation and then keep few of them there. ps: Mean while i suggest to have a look at these great articles from Mark Russinovich https://techcommunity.microsoft.com/t5/windows-blog-archive/pushing-the-limits-of-windows-physical-memory/ba-p/723674
  6. emailx45

    Example of wasteful, innefficient string manipulation

    About Memory Management: by MS https://docs.microsoft.com/en-us/windows/win32/memory/about-memory-management Heap Functions <--- relevant for your topic!!! https://docs.microsoft.com/en-us/windows/win32/memory/heap-functions https://docs.microsoft.com/en-us/windows/win32/memory/low-fragmentation-heap by Jim Diroff II on StackOverFlow summary: https://superuser.com/questions/1579851/what-is-standby-memory-in-ms-windows-os the MSWindows (or others, I dont know), like this: "hey, user, maybe it's cool to have this in memory for later ... let's read, let's read ..."
  7. David Heffernan

    Example of wasteful, innefficient string manipulation

    What's the point? You've already reached a false conclusion based on a flawed investigation. If we try to point this out you tell us that you aren't interested in learning the truth.
  8. emailx45

    Hints on debug and release build

    my tip for test: All compilation depend of your "Project-Option" definition (be by IDE or by file), then, try "delete your .DPROJ" file (backup it of course) to reset it. it will be re-created by IDE when save or compile your project! Later, compare your .DROP now, and before with "Compare Beyound" -- Edit menu!
  9. Pawel Piotrowski

    Example of wasteful, innefficient string manipulation

    Actually, your code is wrong. Now you are doubling the memory usage. SetLength creates a big string, then in the loop, you add more to it... If you wish to pre-allocate the length of the string, then you need to do it this way: Function MultiplyStrWithPreallocate(aStr: String; aMultiplier: Integer): String; Var i, pos: Integer; Begin SetLength(Result, Length(aStr) * aMultiplier); pos := 1; For i := 1 To aMultiplier Do Begin move(aStr[1], Result[pos], Length(aStr) * sizeOf(Char)); Inc(pos, Length(aStr)); End; End; Here is what is going on behind the scene, when you call: Result := Result + aStr; the memory manager creates a new temporary string, assigns the content of "result" and "aStr" to it. That new temporary Strings is then assigned to "result" which in turn decreases the reference counter to the String that result is refering... in that case to 0, which causes the memory manager to release that String. So for a short period of time, both, the original "result" and the "result"+"aStr" are in memory. And for each for-loop-iteration, you basically have a copy operation of the whole string. My above optimization using move reduces both of those concerns. There is just one memory allocation, and only a few bytes are copied in each for-loop-iteration