Karim Belabas on Mon, 20 Aug 2018 11:47:20 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Using lazy vectors in library mode


* Jeroen Demeyer [2018-08-20 10:28]:
> I have a question about using "lazy vectors" such as ell structures.
> 
> It seems to me that the usual programming model manipulating avma may lead
> to memory leaks with such structures. So one should call obj_free() on an
> elliptic curve before resetting avma.

Yes. t_LISTs present analogous problems.

> However, it seems difficult to automate this: as far as I know, a program
> cannot know that ellinit() returns a lazy vector. So I wonder how GP and
> GP2C deal with this. I am asking this in the context of the Python
> interface, where this is probably giving memory leaks currently.

GP deals with it properly when structures are stored in GP variables:
when a variable goes out of scope or when we store a new value, the old
value is destroyed (gunclone_deep(); see e.g. changevalue). Which in
particular recovers the memory used in the "lazy" components, without
explicitly calling obj_free().

Whenever the initialization is "inlined", however, a memory leak occurs:
  ellgroup( ellinit([0,1],Mod(1,5)) );   \\ DEPRECATED: leaks memory...
because the above garbage collector no longer handles the object created
by ellinit, in which ellgroup stored the group structure.

  (11:43) gp > \gm3
     debugmem = 3
  (11:43) gp > {
    print(getheap());
    ellgroup(ellinit([0,1],Mod(1,5)));
    print(getheap());
  }

  [24, 2306]
  new block, size      3 (no 36): 7fbab344ea68
  new block, size     11 (no 37): 7fbab344eaa8
  [26, 2330]
  new block, size      2 (no 38): 7fbab344eb28

Two blocks (36 and 37) are lost for a total of 14 bytes.


Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite de Bordeaux         Fax: (+33) (0)5 40 00 21 23
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~kbelabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`