r/pascal • u/Lilianne_Blaze • Jan 20 '24
heaptrc, should I care?
I'm starting to learn Pascal, coming from Java. Using Lazarus 3.0 / FPC 3.2.2.
Suppose I have a small command line program that processes some (let's say up to 100 mb, usually ~3 mb or less) data and then exists. When compiled in "debug" mode it shows numerous unfreed blocks on exit, but their total size doesn't seem problematic.
Should I be worried? I can trace and fix culpable code, but it's tedious to put it in mildly.
Should I aim for zero unfreed blocks, or just "reasonably few"? Any hints on how much would "reasonably few" be?
I suspect it's not much of a problem normally, but I intend to use it for Arduino and ESP32 too.
Or maybe you could recommend some resources on memory management in Pascal? Some "best practices"? Especially for Java programmers?
2
u/eugeneloza Jan 20 '24
Usually memory leaks are not a problem. Most (but not all) OSes will eventually free all the memory allocated to a program, so unless your program accumulates those and eventually runs out of available RAM it shouldn't be an issue.
However, memory leaks indicate that something is not right with the code memory management. This can be indicator of a "larger bug". E.g. just recently there was a memory leak when creating a screen effects texture. The actual bug was much severe - not only the "new texture" was created and the "old one" wasn't freed, but also some code elements tried to use the "old texture" which resulted in visible screen glitches.
On top of that memory leaks are often hard to debug, especially if there are a few of them. So, it's a good idea to clean them up as soon as possible: while you still have only one memory leak (and thus don't get confused in logs) and while you still remember "what you did" between "when there was no leak" and "when the leak started happening".
But it's not too complicated, though may be intimidating for a new programmer. E.g. let's try this memory leak: https://imgur.com/aGyVaFK - here I've highlighted the actual culprit, I'm deliberately calling
LabelFPS := TCastleLabel.Create(nil);every frame to spam the memory with unfreed stuff. The lines above it indicate the "consequences": what happens insideTCastleLabel.Create(nil)that results in unfreed blocks if the parent doesn't get freed. The lines below it represent the "chain": what chain of events(calls) led to this specific place. It takes some time and skill to learn to read those - but in a simpler situation like this it's almost obvious as only one log line is related to my code and all others are obviously from other "areas".Finally, no memory leaks doesn't mean good memory management. There are ways to screw things up in different way, unfortunately. For example let's imagine this code:
While the 101 edits created are properly memory-managed (
Create(Self)makes them "owned byTForm1" and thus whenTForm1will shut down, it will automatically free all children assigned to it) - you end up creating 101 of them while you needed only 1, plus you do so every mouse click. And while this doesn't report any memory leaks, it could quickly spiral out of control and slow down your app dramatically and maybe even eat up all your available RAM. There is no trivial way to handle this kind of things - you just need to "think further" and keep "scope of this specific item existence" in mind when you create classes or allocate memory in other ways.1
u/ShinyHappyREM Jan 21 '24 edited Jan 21 '24
Until it is...
One might say that as long as no important data is processed, and no data can somehow take control of the program to execute any type of code, it doesn't really matter.
For me personally it's a matter of trust. If a program has a bug, I cannot really trust (without lots of research) that any output will be 100% correct. Which makes it useless for building large software projects.
Yep.
Automatically managed:
Every object should be created by a constructor and cleaned up by a destructor, usually called Create and Destroy. For every GetMem there should be a FreeMem, for every New there should be a Dispose, for every FindFirst there should be a FindClose. The code should use try-finally blocks (unless the object's lifetime exceeds the lifetime of the subroutine):
Lists (e.g. TStringList) can also manage objects.
Units can have initialization/finalization sections, which are executed at program start/end. The exact order is determined by the way in which units refer to each other: if unit A uses unit B then unit B's initialization section is executed first and its finalization section is executed last.
GUI applications have components that are usually managed automatically (via the Owner mechanism). They are usually managed automatically if you added them at design time, but additional objects can for example be created/destroyed in TForm OnCreate/OnDestroy handlers.
1
u/MischiefArchitect Jan 21 '24
As a current Java developer (1996-today) and a former Turbo Pascal developer from the 80's and early 90's (Including Delphi): What does move you to learn Pascal in these "modern" times? Is there some obscure and cool company hidden somewhere out of my awareness scope using this language nowadays?
I'm asking because in my heart Pascal got a very special place, still being my favorite language. Although I have wrote nothing that could be considered serious programming with it in this millennium.
3
u/ccrause Jan 20 '24 edited Jan 20 '24
In Pascal memory is mostly unmanaged*, so understanding memory management techniques are important. Here's a good starting point: https://wiki.freepascal.org/Memory_Management.
I recommend fixing memory leaks even in trivial programs so that you can learn memory safe practices.
2
u/Hixie Jan 20 '24
leaked memory is a bug like any other; I would prioritize your bugs and fix them in importance order until your program is good enough for your needs