View mode: basic / threaded / horizontal-split · Log in · Help
October 01, 2009
A possible leak
Time ago Jon Harrop has found a memory leak in a F# program running on Mono, he has reduced the program to a minimal test case. I have translated that code to D2 and Python2:

-----------------

struct Node {
   int data;
   Node* next;

   this(int data, Node* next) {
       this.data = data;
       this.next = next;
   }
}

void main() {
   Node* lh = new Node(1, null);
   lh.next = lh;

   while (true) {
       Node* lh2 = lh;
       lh2.next = new Node(2, lh2.next);
       lh = lh2.next;
       lh2 = lh;
       lh2.next = lh2.next.next;
   }
}


-----------------

class Node:
   def __init__(self, data, next):
       self.data = data
       self.next = next

def main():
   lh = Node(1, None)
   lh.next = lh

   while True:
       lh2 = lh
       lh2.next = Node(2, lh2.next)
       lh = lh2.next
       lh2 = lh
       lh2.next = lh2.next.next

main()

-----------------

It just creates a circular single-linked list that represents a queue. And then adds an item and pops another out. The popped out items have the "next" field that point to the struct itself.

The D version of the code eats more and more RAM, while the Python version runs at constant memory (probably because CPython GC is a reference count augmented with a cycle detector, that helps in such situations).

This was an answer on the mono list regarding this GC problem (that the dotnet GC doesn't share, so that F# program doesn't leak on dotnet):
http://lists.ximian.com/pipermail/mono-list/2009-February/041236.html

This topic may interest Lucarella too.

Can the D GC be improved to avoid such problem, maybe like the CPython GC? And is such improvement worth it (= useful in practical programs)?

Bye,
bearophile
October 01, 2009
Re: A possible leak
bearophile, el  1 de octubre a las 07:23 me escribiste:
> Can the D GC be improved to avoid such problem, maybe like the CPython
> GC? And is such improvement worth it (= useful in practical programs)?

I've tested it with LDC (using classes instead of structs, because D1
doesn't support struct constructors) and it works with a perfectly stable
memory usage.

-- 
Leandro Lucarella (AKA luca)                      http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
A lo que Peperino respondióles: aquel que tenga sabañones que se los
moje, aquel que padece calvicie no padece un osito, no es bueno comer
lechón en día de gastritis, no mezcleis el vino con la sandía, sacad la
basura después de las ocho, en caso de emergencia rompa el vidrio con
el martillo, a cien metros desvio por Pavón.
	-- Peperino Pómoro
October 01, 2009
Re: A possible leak
Leandro Lucarella:

> I've tested it with LDC (using classes instead of structs, because D1
> doesn't support struct constructors) and it works with a perfectly stable
> memory usage.

That's a different situation, the compiler is different, the data structure is different, and the runtime too may be a little different.
I have tested with LDC with both structs and classes and the two programs don't leak.

Bye,
bearophile
October 01, 2009
Re: A possible leak
bearophile, el  1 de octubre a las 12:39 me escribiste:
> Leandro Lucarella:
> 
> > I've tested it with LDC (using classes instead of structs, because D1
> > doesn't support struct constructors) and it works with a perfectly stable
> > memory usage.
> 
> That's a different situation, the compiler is different, the data structure is different, and the runtime too may be a little different.
> I have tested with LDC with both structs and classes and the two programs don't leak.

Sure, the point was: this is not a problem inherently to the D GC, it's
just a bug in a particular (version of the) compiler you are testing.

-- 
Leandro Lucarella (AKA luca)                      http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Si pudiera acercarme un poco más, hacia vos
Te diría que me tiemblan los dos pies, cuando me mirás
Si supieras todo lo que me costó, llegar
Hoy sabrías que me cuesta respirar, cuando me mirás
Top | Discussion index | About this forum | D home