c - G-wan xbuf_xcat(),难道是内存泄露?

标签 c g-wan

我在 gwan 上遇到关于回复请求的问题,当我调用函数 xbuf_xcat(get_reply(argv), replycontent) 时,RSS 值不断上升。如果我注释掉这个函数或者改成xbuf_xcat(get_reply(argv), "value=1"),这个奇怪的现象就不会发生了...

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

(如果我运行一夜,消耗将近1GB内存...)
任何想法 ??

我修改的代码:

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);

下面是代码内容:(我干脆不使用自己写的函数,但是RSS还是每7-10秒上升一次)

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 结果:

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

如果我将 xbuf_xcat(reply, replycontent); 更改为 printf("reply:%s\n",replycontent); ,RSS 是稳定的,但突然上升仍然发生。

最佳答案

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

如工作 xbuf_xcat(reply, "value=1") 所示,G-WAN 自动回收分配给 reply xbuffer 的内存,所以这不是问题。

您的问题来自您如何生成replycontent

您的问题中缺少这部分代码。如果您需要帮助,展示您正在做的事情可能会有所帮助。


更新(在问题中披露源代码之后)

您的代码使用分配在堆栈上的小缓冲区,因此它不会改变 G-WAN 的内存使用。

此外,您应该直接写入“回复”xbuffer,而不是写入一个临时缓冲区,然后将其复制到“回复”xbuffer - 要进行这样的复制,您应该使用 xbuf_cat() 或 xbuf_ncat() ,但不是 xbuf_xcat()。

鉴于此(无损但毫无意义的)代码,您看到的内存“突然增加”可能来自其他脚本(处理程序?维护脚本?,OS/VM 配置?)中的问题,或者来自您进行的测试,如果您正在使用非常高的并发度。

或许您可以尝试使用 C 语言以外的其他编程语言编写 G-WAN servlet(G-WAN 支持 15 种不同的编程语言,包括 Java、C#、Perl、Python、Ruby 等),这将帮助您避免大多数内存分配陷阱。

关于c - G-wan xbuf_xcat(),难道是内存泄露?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18203292/

相关文章:

mysql - G-WAN 和持久 MySQL 连接

c - 从 G-Wan C 脚本读取 POST 有效负载

c - 通过 gwan 获取图像和交付

c - 如何将 '\0' 转换为 const void*?

c - 将不同类型的指针传递给函数 (C)

c++ - 如何将 64 位 void 指针转换为 32 位 void 指针?

c - 为什么我的 while 循环忽略 NULL 条件?

c++ - 在展开堆栈之前获取回溯

mysql - 在C中将MySQL连接保留在函数中的静态变量中(避免全局变量)

nginx - WebSocket 的 API 网关