Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Emacs Lisp's lexical-let leak memory?

I read about lexical-let's memory leak, for example here: Are there any problems with lexical-let or other cl-macros??? - Users list for the GNU Emacs text editor - ArchiveOrange

It says:

"Note that variables bound with lexical-let are never released, even if they are never used. Try

(loop for i from 1 to 100000 collect (lexical-let ((x i)) '()))

and watch it eat memory."

But I think this code eats memory just because the list made by loop grows. So, I wrote a few elisp codes to check when it occurs but I could not find a example of the leak.

This is how memory grows with time when I execute the code below.

lambda-in-let.el

(require 'cl)

(defvar num-loop-1 30)
(defvar num-loop-2 100000)

(loop for i from 1 to num-loop-1 do
      (loop for j from 1 to num-loop-2 collect
            (lexical-let ((x `(,i ,j))) (lambda () x))))

It looks like there is no leak.

See more examples here: https://gist.github.com/1703325

ADDED: This is how the first example eats memory. As I said, I think it is an artifact.

lambda-in-let.el

like image 506
tkf Avatar asked Jan 30 '12 10:01

tkf


People also ask

When memory leak will happen?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

What causes memory leakage?

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data. As a program operates, it sometimes needs more memory and makes an additional request.

What is a memory leak give an example how it can be avoided?

The memory leak occurs, when a piece of memory which was previously allocated by the programmer. Then it is not deallocated properly by programmer. That memory is no longer in use by the program. So that place is reserved for no reason. That's why this is called the memory leak.


1 Answers

I just found this in emacs-devel:

When does Emacs Lisp's lexical-let leak memory? So... Is it true, that "variables bound with lexical-let are never released, even if they are never used"?

Not that I know. Of course, this code is not bug-free, but I don't know of any concrete case that bumps into such a bug.

-- Re: lexical-let cause memory leaks?

like image 163
tkf Avatar answered Sep 22 '22 12:09

tkf