I do tend to use classes a lot some of which contain strings. I am curious if I change the length of a string stored in the field of a class does that change the amount of memory being used? I think shortstring is limited to 255 chars but string has no fixed length ,so how much memory would be used? Just enough to store it in its current length? Supposed you have a a tmemo and paste a lot of text into it? What happens With memory? Does pasting text allocate memory? Or suppose you create a class containing a tmemo and retrieve the text from an inifile. The class needs less memory before the text is loaded? I’m curious how it all works. If you create another class before loading text to memo it seems like the memory Pertaining to different classes could be intertwined.
Short strings (those with the square brackets [] at the end) are fully contained into the declared variable/record/class. From the memory perspective,
string[N] can be viewed as
array[0..N] of char with zero index being used as the current string length. They are just handled a bit differently than arrays by the compiler (you can concatenate them with + for example). On the other hand, strings declared without square brackets are actually pointers to an internal structure in heap, which holds the contents together with the code-page, current length, reference counter. In the trivial case when the string is empty ('') the pointer is just Nil. They are "managed", i.e. the compiler internally allocates/reallocates/de-allocates them according to the operations applied. They have
copy-on-write semantics.
Using shortstrings have no implications on heap, at least until you explicitly start to allocate memory via pointers for those strings. But they're copied in their entirety when passed as an argument or returned back from a function.
You can intermix both string types in source but that actually it will lead to more heap operations, since the compiler will automatically convert one type to another using temporaries.
If you're using a library with a functions and/or properties defined as a
string (without []), the LCL is such a library, you can't do much about it. But since LCL is for UI, it is not expected the application to work for an indefinite amount of time i.e. it will be closed, restarted, etc. For example web browsers put each tab in a separate process in order to restart them if needed, without affecting the other tabs.
I’ve never done a multithreaded application. What are the circumstances which require multiple threads is it for being able to respond to different events while something else is working?
I vaguely remember a command that could be put into a loop to respond to messages . I can’t remember what it was...
Multithreading is a vast subject which has also close relationships with the memory management, but IMO it shouldn't be involved into the discussion, it will be too much for now.
Edit: A word about the classes: they have a virtual class method
newinstance and a virtual method
FreeInstance which allocates, resp. de-allocates the instance in heap. They can be overridden in order to implement different means of allocation for each class when needed, i.e. to make pools or use a pre-allocated chunks or whatever convenient.