Sockets server program increasingly consumes memory

Sockets server program increasingly consumes memory

Post by Louis Banen » Thu, 18 Jan 2001 21:49:47



Hi,

Platform IBM RS6000 - AIX 4.3.3

I have got a sockets server program that consumes more and more memory while
running. I found out that the increasing memory consumption is due to the
following code within the "while ((c = fgetc(fp)) != EOF) {"  loop. Every
time a request comes in from the client (string of 15 characters) the memory
usage increases. If I remove the while loop than memory is not increased.

Any ideas ?

Regards,

Louis Banens
----------------------------------------------------------------------------
--------------
  if (FD_ISSET(s, &ready)) {
   rstatus = (ns = accept(s, (struct sockaddr*)&fsaun, &fromlen));
   if (rstatus < 0) {
    perror("accept");
    printf("server: accept error: %d\n",rstatus);
    fflush(stdout);
    exit(1);
   }

   fp = fdopen(ns, "r");
   i=0;
   strcpy(input,"");
   while ((c = fgetc(fp)) != EOF) {
    /*putchar(c);*/
    if (c == '\n')
     break;
    input[i] = c;
    i++;
   }
   i++;
   printf("i= %d - input: %s\n",i,input);

   close(ns);

  }
----------------------------------------------------------------------------
--------------

 
 
 

Sockets server program increasingly consumes memory

Post by Rainer Temm » Thu, 18 Jan 2001 22:23:31



>    fp = fdopen(ns, "r");
      ......
>    close(ns);

Hi Louis,

By calling fdopen() you associate a FILE-structure with your
socket-descriptor.
Therefore, you should call "fclose(fp)" instead of "close(ns)".
fclose() will call close() internally.
The way you do it, you close the socket-descriptor, but you'll still have
the
FILE-structure.

Regards Rainer

 
 
 

Sockets server program increasingly consumes memory

Post by Jose Pina Coelh » Sat, 20 Jan 2001 04:19:41




> >    fp = fdopen(ns, "r");
>       ......
> >    close(ns);

> Hi Louis,

> By calling fdopen() you associate a FILE-structure with your
> socket-descriptor.
> Therefore, you should call "fclose(fp)" instead of "close(ns)".
> fclose() will call close() internally.
> The way you do it, you close the socket-descriptor, but you'll still have
> the
> FILE-structure.

Not only it leaves the fp pointing to an invalid FILE structure, it also
doesn't flush any remaining bufered data.
 
 
 

Sockets server program increasingly consumes memory

Post by Rainer Temm » Sat, 20 Jan 2001 17:40:30






> > >    fp = fdopen(ns, "r");
> >       ......
> > >    close(ns);

> > Hi Louis,

> > By calling fdopen() you associate a FILE-structure with your
> > socket-descriptor.
> > Therefore, you should call "fclose(fp)" instead of "close(ns)".
> > fclose() will call close() internally.
> > The way you do it, you close the socket-descriptor, but you'll still
have
> > the
> > FILE-structure.

> Not only it leaves the fp pointing to an invalid FILE structure, it also
> doesn't flush any remaining bufered data.

Flushing the data wouldn't be the main point in this special case, because
he used fdopen() with the "r" flag...
but yes. The File-Structure uses a databuffer, which will remain allocated.

Regards Rainer

 
 
 

1. Limiting the amount of memory a program consumes

I've been trying to cause a program to run out of memory, on a linux
machine. More specifically, I wanted to test what happens when an stl
container runs out of memory.

I wrote a program that uses an stl vector and doubles its size with
each iteration, and then prints the current size of the vector.

Then I used 'limit' to limit datasize, stacksize, and memoryuse to
1Mb.

However, when I ran the program, it printed numbers up to 33554432
before I stopped it. Since I used vector<int>, it means that the
program had by then allocated at least 130Mb of memory.

Could anyone explain what went wrong?

2. How to create Installation boot disk.

3. How can I convert my C socket server program into a simple web server

4. UNIX ADMIN..DOWNTOWN CHICAGO

5. Memory use: which process is consuming it all?

6. Looking for PGP

7. 'ssh node "ls -lR"': consumes all memory on the client!

8. X programming

9. ArcServ agent consuming memory

10. Need to write a c code to find the total memory consumed by a process

11. How to consume memory and load CPU?

12. ESSBASE is being a killing system by consuming all memory.

13. ipop3d & imapd consuming insane amounts of memory