Can using goto
with labels cause memory leaks? All I found in the documentation for goto
that seems relevant is:
The
goto LABEL
form finds the statement labeled with LABEL and resumes execution there.
Is it safe to use goto LABEL
?
DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.
The problem with using goto statements is that it is easy to develop program logic that is very difficult to understand, even for the original author of the code. It is easy to get caught in an infinite loop if the goto point is above the goto call.
"The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided."
Once the memory has been leaked, it's gone for as long as the program is running. Or it wouldn't be a leak. But, if you're referring to the computer system's memory, the answer is: No. Memory is reclaimed when the leaking program ends (perhaps due to crashing once out of memory).
After 1 minute of testing, the answer seems to be: yes no (see update below)
Watching top
while this is running, %MEM
continually increments
{
THIS:
my $x = 1;
goto THIS;
}
This does not exhibit the same incrementing %MEM
counter
while (1) {
my $x = 1;
}
UPDATE
I misunderstood the question. My take on the question was whether memory would be allocated for a lexical variable that already existed in that lexical scope with the use of a goto
, and my test seems to say yes. Strictly speaking, this is not a memory leak. Should perl ever exit this lexical scope, the allocated space would be released.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With