Re: memory leak huge number of digits mpz_mul,mpf_sqrt_ui

2021-02-26 Thread Torbjörn Granlund
Are you famiiar with valgrind?  It is a great tool for detecting memory
leaks.  I used it to oonfirm that GMP indeed does not leak for your test
cases.  (You seem to have forgotten to mention the system you used for
these experiments; I used an amd64 cpu running debian.)

Your analysis of what is a memory leak is incorrect.  You might need to
better understand the role of malloc/free (in the C library) and the
underlying system memory handling functions (sbrk/brk/mmap/munmap).  The
former do not necessarily give back its memory to the system.  That's
not a memory leak by normal definitions.  Or at least, an application
cannot affect this "leak".

Again: No GMP bug.

The GMP lists are not the right place for discussions about how malloc
interfaces to the kernel.  But I am sure there are great explanations
which you will be able to find on the Net.


-- 
Torbjörn
Please encrypt, key id 0xC8601622
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: memory leak huge number of digits mpz_mul,mpf_sqrt_ui

2021-02-26 Thread Paul Zimmermann
   Dear Susumu tsukamoto,

I tried valgrind with the following simplified program and it shows no memory
leak:

$ cat test_mul.c
#include "gmp.h"
int main()
{
long int num = 20; // 200,000,000 
long int ik;
mpz_t DC;
printf(" start:  num= %'ld \n", num);

mpz_init_set_str(DC, "1000",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);
}

$ /usr/bin/g++ -g -Wall test_mul.c -o test_mul01 -lgmp
$ valgrind ./test_mul01
...
==23406== HEAP SUMMARY:
==23406== in use at exit: 0 bytes in 0 blocks
==23406==   total heap usage: 17 allocs, 17 frees, 231,296 bytes allocated
==23406== 
==23406== All heap blocks were freed -- no leaks are possible

Does valgrind report a memory leak with your original value num=2?

Best regards,
Paul Zimmermann
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


memory leak huge number of digits mpz_mul,mpf_sqrt_ui

2021-02-25 Thread cents2823
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@tsuka:~/sqrt$ g++  -Wall test_mul.c -o 
test_mul01 -lgmp
tsuka@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@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 int main()
{ 
    long int num = 2; // 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, "1000",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 
To: cents2...@yahoo.co.jp 
Cc: "gmp-bugs@gmplib.org" 
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.


-- 
Torbjörn
Please encrypt, key id 0xC8601622

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: memory leak huge number of digits mpz_mul, mpf_sqrt_ui

2021-02-25 Thread Torbjörn Granlund
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.


-- 
Torbjörn
Please encrypt, key id 0xC8601622
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


memory leak huge number of digits mpz_mul, mpf_sqrt_ui

2021-02-25 Thread cents2823
Dear GMP developers,
I am making a program that calculate pi using GMP.
There is a memory leak that seems to be the cause of GMP.
It seems that a memory leak occurs when a huge number of digits
 multiplying and square root calculation.
The following test program(test_mul.c, test_sqrt.c) causes a memory leak.
Run vmstat 5 -SM on another terminal.
Run the program,vmstat memory free value at "clear end any key in so end"
 output ,and vmstat memory free value after enterring key,
differrence between the value is memory leak.
My result of changing the number of multiplication digits by 
several patterns at test_mul.c:
num= 2*10^8 : memory leak= 32MiB
num= 10^8    : memory leak= 25MiB
num= 5*10^7 : memory leak= 12MiB
num= 4*10^8 : memory leak= 48MiB
num= 8*10^8 : memory leak= 48MiB
Similary my result of canging the number of mpf_sqrt_ui digits
by several patterns at test_sqrt.c:
num= 10^9    : memory leak= 51MiB
num= 4*10^8 : memory leak= 41MiB
num= 2*10^8 : memory leak=  0Mib
num= 10^8    : memory leak=  0MiB
num= 2*10^9 : memory leak= 50MiB
Test bed:
ubuntu 20.04.2 LTS
GMP 6.2.0
gcc 9.3.0
Best regarde.
Susumu Tsukamoto
/* test_mul.c g++  -Wall test_mul.c -o test_mul01 -lgmp*/#include "gmp.h"
#include int main()
{ 
    long int num = 2; // 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, "1000",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);
}
/* test_sqrt.c g++ -Wall test_sqrt.c -o test_sqrt01 -lgmp*/#include "math.h"
#include "gmp.h"
#include int main()
{  
    long int num = 4; // 400,000,000
    long int lims;
    setlocale(LC_ALL,"ja_JP.utf-8");    printf(" start:  
mpf_sqrt_ui(CC) num= %'ld \n", num);
 
    mpf_t CC;  
    lims = num/((log(2)*64)/log(10));
    mpf_set_default_prec(lims*64); 
    mpf_init(CC);      mpf_sqrt_ui(CC, 2);
    
    mpf_clear(CC);
 
    printf(" clear end any key in so end\n");
    char dm[5];
    scanf("%s", dm);
}
___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs