help with memory usage

Marc Glisse marc.glisse at
Tue Mar 10 20:21:08 CET 2009

On Mon, 9 Mar 2009, Christ Schlacta wrote:

> I've got the following simple struct:
> class prime_t { //prime list element type.
>  public:
>    prime_t();
>    mpz_class *value;
>    prime_t *next;
> };

I believe you should store the mpz_class directly, not a pointer to it (a 
mpz_class is not much more than a pointer itself). It will save some 
amount of overhead and simplify the code (no need for new/delete).

> The problem:  I can get several hundred megabytes worth of prime
> numbers..  250MB at my last count.  But in memory, 250MB of prime
> numbers (stored as newline-delimited strings) ends up being more than
> 1600MB in memory using this structure.

That is a lot...

Are many of your prime numbers small enough that they could be stored as 
unsigned long?

You could also stop relying on the default memory allocator and either 
tell gmp to use some other allocation function or allocate the buffers 
yourself (for read-only never-deallocate numbers like your prime numbers 
seem to be this should not be hard).

Marc Glisse

More information about the gmp-discuss mailing list