memory leak huge number of digits mpz_mul,mpf_sqrt_ui

cents2823 at cents2823 at
Fri Feb 26 02:42:04 UTC 2021

Thank you for your response.
Your answer is difficult and I don't understand well.
I am making a program to calculate pi, and when I calculate a huge number of digits pi, the calculation is finished and all the variables are released, but a lot of memory remains in use. 
I set a release flag for each variable and checked it programmatically to see if there was any omission of variable release, but there was no omission of variable release.In fact, the memory was low and execution was almost stopped at the end of the program. This is a symptom of a memory leak.

Shows test_mul.c execution status.tsuka at tsuka:~/sqrt$ g++  -Wall test_mul.c -o test_mul01 -lgmp
tsuka at tsuka:~/sqrt$ ./test_mul01
 start:  num= 200,000,000 
 clear end any key in so end <--At this time vmstat memory free value is 62606.It is the same value even if
 I wait for 1 minute.
0 <-- At this time vmstat memory free value is 62639,62639-62606=33MiB is memory leak
tsuka at tsuka:~/sqrt$ 
Test_mul.c is shown below again,but since gmp allocates alarge memory,the memory leak seems to be caused by gmp.
Best regards.Susumu Tsukamoto
/* test_mul.c g++  -Wall test_mul.c -o test_mul01 -lgmp*/#include "gmp.h"
#include <locale.h>int main()
    long int num = 200000000; // 200,000,000 
    long int ik;
    char dm[5];
    setlocale(LC_ALL,"ja_JP.utf-8");    mpz_t DC;        
    printf(" start:  num= %'ld \n", num);
    mpz_init_set_str(DC, "10000000000000000000",10); // DC=10^19
    ik = 19;        // DC=10^ik set   num/2 <= ik < num
    while (ik * 2 < num) {     
        mpz_mul(DC, DC, DC);
        ik = ik * 2;
    }                   mpz_clear(DC); 
    printf(" clear end any key in so end \n");
    scanf("%s", dm);

Susumu tsukamoto

 ----- Original Message -----
From: Torbjörn Granlund <tg at>
To: cents2823 at 
Cc: "gmp-bugs at" <gmp-bugs at>
Date: 2021/2/25, Thu 20:22
Subject: Re: memory leak huge number of digits mpz_mul, mpf_sqrt_ui
Your analysis is very likely incorrect.

You may cause the same "leak" without GMP.  Just malloc a set of large
chunks of different sizes.  Access them to make sure it is actually
there, then free them in some random order.  Often the free calls will
not have given back the memory to the system.  Still, that's no memory
leak in the sense you seem to suggest.

Please encrypt, key id 0xC8601622

More information about the gmp-bugs mailing list