GMP library memory issue

Hi everyone,

I'm trying out the gmp library by building a simple pi calculation program (original, I know!). On a million digits of Pi I've debugged the program and seem to have about a megabyte too much of memory at the end of the program (I start with around 250k before any allocation begins and end at around 1200). I'm sure I've clear all the objects but I can't see where this memory usage is coming from.

I've attached my code.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

int main(int argc, char *argv[])
{
//set a//

	int digitsofpi =1000000;

        mpf_set_default_prec(log2(10) *digitsofpi );

        mpf_t a;
        mpf_init (a);
        mpf_set_ui (a,1);


    //set b//
        mpf_t b;
        mpf_init (b);

        mpf_t b1;
        mpf_init (b1);
        mpf_sqrt_ui (b1,2);

        mpf_ui_div(b,1,b1);


    //set t//

        mpf_t t;
        mpf_init (t);
        mpf_set_d (t,0.25);


    //set x//

        mpf_t x;
        mpf_init (x);
        mpf_set_ui(x,1);

    //calc//


    int roundstodo= log2(digitsofpi);

        for (int f=0;f<( roundstodo );f++)
        {
            int totalrounds = log2(digitsofpi);


            //set y//
            mpf_t y;
            mpf_init (y);
            mpf_set (y,a);


            //change a//

            mpf_t a1;
            mpf_init (a1);
            mpf_add(a1,a,b);

            mpf_div_ui (a,a1,2);


            // change b//

            mpf_t b2;
            mpf_init (b2);
            mpf_mul(b2,b,y);


            mpf_sqrt (b,b2);



            //set t//

            mpf_t t1;
            mpf_init(t1);
            mpf_sub(t1,y,a);

            mpf_t t2;
            mpf_init (t2);
            mpf_pow_ui (t2,t1,2);

            mpf_t t3;
            mpf_init(t3);
            mpf_mul(t3,x,t2);

            mpf_sub(t,t,t3);


            //change x//


            mpf_mul_ui(x,x,2);





            // DELETE

            mpf_clear (y);
            mpf_clear (a1);
            mpf_clear (b2);
            mpf_clear (t1);
            mpf_clear (t2);
            mpf_clear (t3);

			cout <<f<<endl;

        }




            // calculate pi//
//
            mpf_t piup1;
            mpf_init(piup1);
            mpf_add(piup1,a,b);

            mpf_t piup2;
            mpf_init (piup2);
            mpf_pow_ui(piup2,piup1,2);         //final up

            mpf_t pidown;                   //final down
            mpf_init (pidown);
            mpf_mul_ui(pidown,t,4);

            mpf_t pifinal;
            mpf_init (pifinal);
            mpf_div(pifinal,piup2,pidown);





     //  mp_exp_t *exp  = new mp_exp_t;
//        std::string *testout = new string(mpf_get_str(NULL, exp, 10, digitsofpi, pifinal));






        //CLEAR
        mpf_clear (piup1);
        mpf_clear (piup2);
        mpf_clear (pifinal);
        mpf_clear (pidown);
        mpf_clear (a);
        mpf_clear (b);
        mpf_clear (b1);
//
        mpf_clear (t);
        mpf_clear (x);

}
Last edited on
What are you using to benchmark ram usage?
I remember from testing code myself that ram consumption may go up even when nothing is allocated during heavy CPU usage. Maybe that's all there is to it. What you can try (if you're afraid of a memory leak) is to put your entire program in a loop. Then you can check for a memory leak.

Btw, I think GMP also has a C++ version (mpf_class), maybe that's better suited.
seem to have about a megabyte too much of memory at the end of the program


Are you joking or what ?
Topic archived. No new replies allowed.