Memory cannot be released

Torbjorn Granlund tg at gmplib.org
Thu Oct 27 10:48:04 CEST 2011


"afendee" <afendee at fsktm.upm.edu.my> writes:

  I am currently running a C++ program having GMP functions in it. I am
  having problem that the problem stops by itself after some times, I
  suspects it was due to memory exhaustion problem.
  
  My program that failed looks like this.
  =================================================================================
  int main()
  {
      int base=2;
      int degree=22;
  
      mpz_t composite, etx, stx, N; 
      mpz_init(composite);
      mpz_init(etx);
      mpz_init(stx);
      
      mpz_ui_pow_ui(composite, base, degree);
      mpz_ui_pow_ui(stx, base, degree);
      mpz_add_ui(stx, stx, 1);
      mpz_ui_pow_ui(etx, base, degree+1);
      mpz_add_ui(etx, etx, 1);
  
      mpz_init_set_ui(N, 2);
  
      for (mpz_set(composite,stx); mpz_cmp(composite,etx); mpz_add_ui(composite, composite, 1)){
              count++;
              bin=dec2baseN(composite, N);
            
      }
  
     mpz_clear(composite);  
     mpz_clear(etx);  
     mpz_clear(stx); 
     mpz_clear(N); 
  }
  ================================================================================
  
  When i ran this problem for degree greater than 21, the program starts
  to fail in the midst of execution. I turn on 'memory usage windows'
  and it shows that the consumption is growing very rapidly. I think
  this is due to the 'variable' composite that I have for the 'for'
  loop.
  
I strongly doubt 'composite' will need much memory, unless the initial
value 'etx' is huge.  You just add 1 to 'composite' each iteration,
which will make 'composite' grow its memory needs extremely slowly.

-- 
Torbjörn


More information about the gmp-bugs mailing list