Ticket #4380 (closed defect: fixed)

Opened 5 years ago

Last modified 5 years ago

[with patch, positive review] fix Memory Leak in libsingular's reduce()

Reported by: SimonKing Owned by: malb
Priority: major Milestone: sage-3.2
Component: commutative algebra Keywords: memory leak libsingular
Cc: Work issues:
Report Upstream: Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description

At  http://groups.google.com/group/sage-support/browse_thread/thread/b997f95c1e2503e0/5db5f9e7d4c8faf2 was discussion about a memory leak. I found a reasonably short bit of code that triggers the leak.

In test.pyx:

from sage.all import copy

def Test(I):
    R=I.ring()
    p=R.random_element()
    J0=list(I.reduced_basis())
    while(1):
        J = copy(J0)
        for i in range(100):
            q=p.reduce(J)
            J.append(q)

Apparently the memory consumption should not grow, since J returns to its original state after finishing the for loop. However, the following Sage session is leaking (i.e., the memory consumption grows rapidly, and after a few seconds 1GB are filled up):

sage: attach test.pyx
Compiling /home/king/Projekte/f5/test.pyx...
sage: from sage.rings.ideal import Cyclic
sage: R=PolynomialRing(QQ,'x',5)
sage: I=Cyclic(R).homogenize()
sage: Test(I)

The while loop comprises

  1. copying of a Sequence (J0) of MPolynomial_libsingular
  2. reduction of MPolynomial_libsingular
  3. appending to a Sequence.

In one of these three steps must be the leak. I suspect it is the reduction and will try to verify it.

Attachments

libsingularLeak.patch Download (841 bytes) - added by SimonKing 5 years ago.
Fixing the leak, after a suggestion of malb

Change History

comment:1 follow-up: ↓ 2 Changed 5 years ago by SimonKing

Sorry, it is not copying a Sequence but a list. But that shouldn't matter.

comment:2 in reply to: ↑ 1 Changed 5 years ago by SimonKing

... and the line J.append(q) (that would occur, e.g., in a Groebner basis computation) is not needed either. Replacing Test(I) by the following code triggers the leak as well:

def Test(I):
    R=I.ring()
    p=R.random_element()
    J=list(I.reduced_basis())
    while(1):
        q=p.reduce(J)

comment:3 Changed 5 years ago by SimonKing

  • Summary changed from Memory Leak in libsingular to [with patch, needs review, needs more work] Memory Leak in libsingular

The patch libsingularLeak.patch partially solves the problem.

I observed that when a coercion error occurs an intermediately generated ideal _I is explicitly destroyed with id_Delete(&_I,r). But when there is no error and the result is returned, then _I is not deleted. But I think it should.

With the patch, the above Test(I) still shows an increase of memory, but a rather moderate increase. So, it seems to be a partial solution of the problem, but more needs to be done.

comment:4 Changed 5 years ago by mabshoff

  • Summary changed from [with patch, needs review, needs more work] Memory Leak in libsingular to [with patch, needs review] Memory Leak in libsingular

Simon,

nice work. If you find any more issues please open a new ticket, so that this patch can be merged. I will review this patch shortly.

Cheers,

Michael

Changed 5 years ago by SimonKing

Fixing the leak, after a suggestion of malb

comment:5 follow-up: ↓ 6 Changed 5 years ago by SimonKing

Since the announced review of Michael didn't come yet, I hope it is ok that I changed the patch according to a suggestion of malb, without opening a new ticket.

The new patch seems to fix it completely.

comment:6 in reply to: ↑ 5 Changed 5 years ago by mabshoff

Replying to SimonKing:

Since the announced review of Michael didn't come yet, I hope it is ok that I changed the patch according to a suggestion of malb, without opening a new ticket.

Yep, I am testing that patch now.

The new patch seems to fix it completely.

:)

Cheers,

Michael

comment:7 follow-up: ↓ 8 Changed 5 years ago by mabshoff

This patch also seems to fix the problem with sr.py consuming vast amounts of memory, but I will do some independent testing first.

Cheers,

Michael

comment:8 in reply to: ↑ 7 Changed 5 years ago by mabshoff

Replying to mabshoff:

This patch also seems to fix the problem with sr.py consuming vast amounts of memory, but I will do some independent testing first.

Unfortunately it doesn't fix the segfault (see #3758), but the amount of memory allocated goes down significantly already.

Cheers,

Michael

Cheers,

Michael

comment:9 Changed 5 years ago by mabshoff

  • Summary changed from [with patch, needs review] Memory Leak in libsingular to [with patch, positive review] fix Memory Leak in libsingular's reduce()

Valgrinds clean, passes doctests, positive review.

Cheers,

Michael

comment:10 Changed 5 years ago by mabshoff

  • Status changed from new to closed
  • Resolution set to fixed
  • Milestone changed from sage-3.2.1 to sage-3.2

Merged in Sage 3.2alpha2

Note: See TracTickets for help on using tickets.