Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can `goto LABEL` cause a memory leak?

Tags:

memory

perl

goto

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?

like image 549
yoniyes Avatar asked Sep 22 '16 21:09

yoniyes


People also ask

What is the main cause of memory leaks?

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.

What is the problem with GOTO statement?

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.

Is goto inefficient?

"The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided."

Do memory leaks go away?

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).


1 Answers

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.

like image 164
Joshua Avatar answered Sep 28 '22 20:09

Joshua