G-wan xbuf_xcat(),它是内存泄漏吗?

时间:2013-08-13 07:39:51

标签: 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
  根20365 0.5 0.8 403848 6496 pts / 0 Sl + 15:07 0:00 | _ / opt / gwan / gwan
  根20365 0.5 0.8 403848 6500 pts / 0 Sl + 15:07 0:00 | _ / opt / gwan / gwan
  根20365 0.5 0.8 403848 6504 pts / 0 Sl + 15:07 0:00 | _ / opt / gwan / gwan
  根20365 0.6 0.8 403848 6504 pts / 0 Sl + 15:07 0:00 | _ / opt / gwan / gwan
  根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
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 0.7 555392 5756 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 1.0 555392 7676 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根8170 0.3 1.0 555392 7676 pts / 0 Sl + 10:29 0:00 | _ / opt / gwan / gwan
  根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会稳定,但仍会突然上升。

2 个答案:

答案 0 :(得分:0)

  

使用xbuf_xcat(get_reply(argv), replycontent),RSS值会增加   使用xbuf_xcat(get_reply(argv), "value=1"),内存使用率是稳定的

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

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

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


更新(根据问题中的源代码披露)

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

此外,您应该直接写入'reply'xbuffer而不是写入临时缓冲区,然后将其复制到'reply'xbuffer中 - 并且要创建这样的副本,您应该使用xbuf_cat()或xbuf_ncat() ,但不是xbuf_xcat()。

考虑到这种(无懈可击但毫无意义)代码,您看到的内存“突然升高”可能来自其他脚本(处理程序?维护脚本?,OS / VM配置?)中的问题,或者来自您所做的测试你使用的是非常高的汇率。

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

答案 1 :(得分:-1)

这看起来像是经典的内存泄漏。

请注意,(在我看来,奇怪的名称)格式化函数xbuf_xcat()函数将动态分配内存来保存结果文本。

如果您未通过传递给xbuf_free()的{​​{1}}上的xbuf_t,则会泄露内存。