Escribir en syslog hace que la asignación de almacenamiento dinámico aumente constantemente de tamaño

Yesterday I was wondering why the number of allocs and frees on the heap kept creeping up: ¿Valgrind comprende el aumento de bytes asignados en el resumen del montón?

Today I have found the culprit, however I do not understand why:

openlog ("XYSV", LOG_PID, LOG_SYSLOG);
syslog (LOG_NOTICE, "server started by %s(%d) on port %d", getenv("USER"),getuid (),servPort);
closelog();

for (;;) /* Run forever */
{
    clntSock = AcceptTCPConnection(servSock);
    char ipstr[INET6_ADDRSTRLEN];

    /* Fork child process and report any errors */
    if ((processID = fork()) < 0)
        DieWithError("fork() failed");
    else if (processID == 0)  /* If this is the child process */
    {
        close(servSock);   /* Child closes parent socket */
        HandleTCPClient(clntSock);

        exit(0);           /* Child process terminates */
    }

    openlog ("XYSV", LOG_PID, LOG_SYSLOG);
    //syslog(LOG_INFO,"%d : created new child process for connect from %s\n", (int) processID,ipstr);
    //syslog(LOG_INFO, "test");
    closelog();

    close(clntSock);       /* Parent closes child socket descriptor */
    childProcCount++;      /* Increment number of outstanding child processes */    
}

As soon as I uncomment the two syslog entries the allocs on the heap (reported by Valgrind) keep increasing. The allocs are also freed, however the total number of allocs keeps increasing and thus the size of the allocated memory. IMHO this would sooner or later cause the server to run out of mem.

My question is: why would this write to the syslog cause the allocs to increase. I am closing the syslog nicely. The first syslog does not have an effect on the heap allocation. When I leave the open and close in place for the second write to the syslog and leave the actual write commented out everything is allright. As soon as I uncomment one or two of the syslog lines the allocs start creeping up.

I must be overseeing something obvious.

preguntado el 31 de julio de 12 a las 10:07

eliminar el fork and check if the memory increases. -

It's not the fork, it's the calls to the syslog. As soon as I uncomment either one of the two syslog messages memory starts to increase. I do not understand why it happens in the lower syslog call and not in the upper one? -

1 Respuestas

Los programas syslog call will allocate memory to hold the log message before delivering it if the message is larger than its local buffer.

It actually writes to a FILE * that was created by a call to open_memstream, which is a C file stream that actually uses a buffer that is automatically grown as required. It then takes the buffer and sends its contents to the logger.

La fuente es aquí.

Respondido 31 Jul 12, 10:07

Thanks, but should closelog not free the buffer? The message I'm sending is short ("test") and the call to closelog() comes after the actual writing. - lleto

@lleto: Your valgrind output had already shown all allocations were matched by frees. I thought you were asking why the allocation count increased. - jxh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.