Memory leakage in set class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frustrated

    Memory leakage in set class

    Before I begin, I must first make the following disclaimer: Although I have
    considerable programming experience, I do not consider myself by any means
    to be an expert C++ programmer. The following may be nothing more than a
    relection of my ignorance. If what I describe is not an actual bug, I would
    be very appreciative if you could briefly explain to me how I can de-allocate
    memory allocated by a set class, since everything I have tried is in vain
    and every computer scientist I have asked seems as dumbfounded as I.


    I am running g++ 2.96 on a i386 redhat linux platform. I think I discovered
    a bug. I compiled and ran the following program.

    #include <set>

    int main()
    {
    unsigned long j;
    set<unsigned long> *o = new set<unsigned long>();
    for(j = 1; j <= 1000000; j++)
    {
    (*o).insert(j);
    }
    (*o).clear();
    delete o;
    while(1);
    }

    Using top, I monitored memory usage and noticed the delete operation did
    not free the 24 MB allocated by the multiple calls to insert in the for
    loop. This problem seems confined to the set and map classes. No matter
    what I seem to do, I cannot de-allocate memory allocated by the set or map
    classes. I do not enounter the problem with the vector class. For example,
    I did not observe using top any memory leakage when I compiled and ran

    #include <vector>

    int main()
    {
    unsigned long j;
    vector<unsigned long> *o = new vector<unsigned long>();
    for(j = 1; j <= 10000000; j++)
    {
    (*o).push_back( j);
    }
    (*o).clear();
    delete o;
    while(1);
    }

    I know that clear alone at least for a vector does not de-allocate memory
    since it merely erases the elements without altering the capacity.
    Nevertheless, shouldn't the delete operation, whether it is applied to
    an empty vector, set, or map, always perform the necessary de-allocation?
  • Marcin Vorbrodt

    #2
    Re: Memory leakage in set class

    "frustrated " <mmm37@cornell. edu> wrote in message
    news:96472676.0 310102031.9f793 1f@posting.goog le.com...[color=blue]
    > Before I begin, I must first make the following disclaimer: Although I[/color]
    have[color=blue]
    > considerable programming experience, I do not consider myself by any means
    > to be an expert C++ programmer. The following may be nothing more than a
    > relection of my ignorance. If what I describe is not an actual bug, I[/color]
    would[color=blue]
    > be very appreciative if you could briefly explain to me how I can[/color]
    de-allocate[color=blue]
    > memory allocated by a set class, since everything I have tried is in vain
    > and every computer scientist I have asked seems as dumbfounded as I.
    >
    >
    > I am running g++ 2.96 on a i386 redhat linux platform. I think I[/color]
    discovered[color=blue]
    > a bug. I compiled and ran the following program.
    >
    > #include <set>
    >
    > int main()
    > {
    > unsigned long j;
    > set<unsigned long> *o = new set<unsigned long>();
    > for(j = 1; j <= 1000000; j++)
    > {
    > (*o).insert(j);
    > }
    > (*o).clear();
    > delete o;
    > while(1);
    > }[/color]

    Try this:

    int main()
    {
    for(;;) {
    unsigned long j;
    set<unsigned long> *o = new set<unsigned long>();
    for(j = 1; j <= 1000000; j++)
    {
    (*o).insert(j);
    }
    (*o).clear();
    delete o;
    }
    while(1);
    }

    Or this:

    int main()
    {
    set<unsigned long> *o = new set<unsigned long>();
    for(;;) {
    unsigned long j;
    for(j = 1; j <= 1000000; j++)
    {
    (*o).insert(j);
    }
    (*o).clear();
    }
    delete o;
    while(1);
    }

    Both should run just fine (in the infinite loop) and not cause any memory
    leaks. I think "top" does not report memory has been dealocated because of
    OS caching nonsense going on behind the scenes. If you run those two
    samples, i think eventually system will start flushing its cache... this is
    a wild guess though:-)


    Martin

    [color=blue]
    >
    > Using top, I monitored memory usage and noticed the delete operation did
    > not free the 24 MB allocated by the multiple calls to insert in the for
    > loop. This problem seems confined to the set and map classes. No matter
    > what I seem to do, I cannot de-allocate memory allocated by the set or map
    > classes. I do not enounter the problem with the vector class. For example,
    > I did not observe using top any memory leakage when I compiled and ran
    >
    > #include <vector>
    >
    > int main()
    > {
    > unsigned long j;
    > vector<unsigned long> *o = new vector<unsigned long>();
    > for(j = 1; j <= 10000000; j++)
    > {
    > (*o).push_back( j);
    > }
    > (*o).clear();
    > delete o;
    > while(1);
    > }
    >
    > I know that clear alone at least for a vector does not de-allocate memory
    > since it merely erases the elements without altering the capacity.
    > Nevertheless, shouldn't the delete operation, whether it is applied to
    > an empty vector, set, or map, always perform the necessary de-allocation?[/color]


    Comment

    • Klaus Eichner

      #3
      Re: Memory leakage in set class

      "frustrated " <mmm37@cornell. edu> wrote in message
      news:96472676.0 310102031.9f793 1f@posting.goog le.com...

      [Snip]
      [color=blue]
      > If what I describe is not an actual bug, I would be very
      > appreciative if you could briefly explain to me how
      > I can de-allocate memory allocated by a set class[/color]

      [Snip]
      [color=blue]
      > I am running g++ 2.96 on a i386 redhat linux platform
      >
      > #include <set>
      >
      > int main()
      > {
      > unsigned long j;
      > set<unsigned long> *o = new set<unsigned long>();
      > for(j = 1; j <= 1000000; j++)
      > {
      > (*o).insert(j);
      > }
      > (*o).clear();
      > delete o;
      > while(1);
      > }
      >
      > Using top, I monitored memory usage and noticed the delete operation did
      > not free the 24 MB allocated by the multiple calls to insert in the for
      > loop.[/color]

      This is a problem, but this is not a bug as far as the standard is
      concerned.
      Jim Hyslop gave a reply to that problem in comp.lang.c++
      (Subject: 'What does delete do with memory?' Date 12 Mai 2000,
      see http://tinyurl.com/qjjy)

      You might find a solution to your problem in your g++ 2.96/Linux
      documentation.



      Comment

      Working...