Question

I meet a problem about reply request on gwan, while I call function xbuf_xcat(get_reply(argv), replycontent), the RSS value keep rising up. If I comment this function or change to xbuf_xcat(get_reply(argv), "value=1"), this strange phenomenon won't happen...

root 20365 0.5 0.8 403848 6468 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan
root 20365 0.5 0.8 403848 6488 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan
root 20365 0.5 0.8 403848 6492 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan
root 20365 0.5 0.8 403848 6496 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan
root 20365 0.5 0.8 403848 6500 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan
root 20365 0.5 0.8 403848 6504 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan
root 20365 0.6 0.8 403848 6504 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan
root 20365 0.6 0.8 403848 6528 pts/0 Sl+ 15:07 0:00 | _ /opt/gwan/gwan

(If I run it overnight, consume nearly 1GB memory...)
Any idea ??

The code I modify:

xbuf_t *reply = get_reply(argv);
xbuf_t f;
xbuf_init(&f);
xbuf_cat(&f,replycontent);
xbuf_ncat(reply, f.ptr, f.len);
xbuf_free(&f);

Below are the code content: (I simply it not to use the function I write, but RSS still rising for every 7-10 sec)

int main(int argc, char *argv[]){
    printf("G-wan start Serving...\n");
    char replycontent[1024];

    //set replycontent value
    strcpy(replycontent, "[");
    int i;
    for( i=0; i<2; i++){
        strcpy(replycontent, "TEST ONLY");
        strcat(replycontent, ",");
    }
    replycontent[strlen(contents)-1] = ']';

    xbuf_t *reply = get_reply(argv);
    xbuf_xcat(reply, replycontent);
    return 200;
}

RSS result:

root 8170 0.3 0.7 555392 5748 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5748 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5748 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5748 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 0.7 555392 5756 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 1.0 555392 7676 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 1.0 555392 7676 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 1.0 555392 7676 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 1.0 555392 7680 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 1.0 555392 7680 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan
root 8170 0.3 1.0 555392 7684 pts/0 Sl+ 10:29 0:00 | _ /opt/gwan/gwan

If I change xbuf_xcat(reply, replycontent); to printf("reply:%s\n",replycontent); , the RSS is stable, but suddenly rise still occur.

Was it helpful?

Solution

With xbuf_xcat(get_reply(argv), replycontent), RSS value grows With xbuf_xcat(get_reply(argv), "value=1"), memory usage is stable

As shown by the working xbuf_xcat(reply, "value=1"), G-WAN automatically recycles the memory allocated for the reply xbuffer, so this is not the problem.

Your problem rather comes from how you are generating the replycontent.

And this portion of the code is missing in your question. If you are asking for assistance, it might help to show what you are doing.


UPDATE (following the source code disclosure in the question)

Your code is using a small buffer allocated on the stack so it does not change the memory usage of G-WAN.

Besides, you should directly write into the 'reply' xbuffer rather than into a temporary buffer which is then copied into the 'reply' xbuffer - and to make such a copy, you should rather use xbuf_cat() or xbuf_ncat(), but not xbuf_xcat().

Given this (undamaging but pointless) code, the "sudden raise" of memory you have seen could come from problems in other scripts (handlers? maintenance script?, OS/VM configuration?), or from the tests you make if you are using very high concurrencies.

Maybe you could try writing G-WAN servlets in another programming language than C (G-WAN supports 15 different programming languages, including Java, C#, Perl, Python, Ruby, etc.), this would help you to avoid most memory allocation pitfalls.

OTHER TIPS

This looks like a classical memory leak.

Note that the (in my opinion, weirdly-named) formatting function xbuf_xcat() function will dynamically allocate memory to hold the resulting text.

If you don't call xbuf_free() on the xbuf_t passed to xbuf_xcat(), the memory is leaked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top