我的程序试图从后端服务器获取两组userid列表(userid之间用换行符分隔),然后将userid放到redis的set中程序运行逻辑没问题,但是发现运行后nginx worker的内存占用率很高代码:local iredis = require('ssp.models.iredis')local order_id = 18622local userlist_files = {"condition/uid/18622_1.txt", "condition/uid/18622_2.txt"} --每个txt文件大概5Mlocal user_lists={}for _, file in ipairs(userlist_files) do local res = ngx.location.capture('/backend', {method=ngx.HTTP_GET, vars={backend_host="http://d.ssp.xunlei.com", backend_uri="/public/upload/"..file}}) table.insert(user_lists, res.body)endlocal red = iredis:new()red:init_pipeline()local key = "ad:o:u:" .. order_idred:del(key)for _, userlist in ipairs(user_lists) do local it, err = ngx.re.gmatch(userlist, "[0-9]+") if not it then log(ngx.ERR, "gmatch err: ", err) else while true do local m, err = it() if err then log(ngx.ERR, err) break end if not m then -- no match found (any more) break end -- found a match red:sadd(key, m[0]) end endendred:expire(key, oneday)local ok, err = red:commit_pipeline()nginx 的/backend配置location /backend { internal; rewrite_by_lua 'ngx.req.set_uri(ngx.var.backend_uri)'; proxy_http_version 1.1; proxy_set_header Connection KeepAlive; proxy_set_header CLIENTADDR $remote_addr; proxy_set_header Content-Type application/x-www-form-urlencoded; proxy_pass $backend_host; }iredis其实是温总的那个redis二次封装的代码不知道哪里有问题,每次执行完之后,内存从50m跳到900m --
local iredis = require('ssp.models.iredis')local order_id = 18622local userlist_files = {"condition/uid/18622_1.txt", "condition/uid/18622_2.txt"} --每个txt文件大概5Mlocal user_lists={}for _, file in ipairs(userlist_files) do local res = ngx.location.capture('/backend', {method=ngx.HTTP_GET, vars={backend_host="http://d.ssp.xunlei.com", backend_uri="/public/upload/"..file}}) table.insert(user_lists, res.body)endlocal red = iredis:new()red:init_pipeline()local key = "ad:o:u:" .. order_idred:del(key)for _, userlist in ipairs(user_lists) do local it, err = ngx.re.gmatch(userlist, "[0-9]+") if not it then log(ngx.ERR, "gmatch err: ", err) else while true do local m, err = it() if err then log(ngx.ERR, err) break end if not m then -- no match found (any more) break end -- found a match red:sadd(key, m[0]) end endendred:expire(key, oneday)local ok, err = red:commit_pipeline()
location /backend { internal; rewrite_by_lua 'ngx.req.set_uri(ngx.var.backend_uri)'; proxy_http_version 1.1; proxy_set_header Connection KeepAlive; proxy_set_header CLIENTADDR $remote_addr; proxy_set_header Content-Type application/x-www-form-urlencoded; proxy_pass $backend_host; }
引用春哥历史回复内容:建议使用类似 lgcstat 这样的工具,统计min, max, count, sum 这些指标,同时确保各类 GCobj 的总空间加起来等于 GC 分配的总空间大小: https://github.com/openresty/nginx-gdb-utils#lgcstat结合类似 lgcpath 这样的工具可以确定大的 GCobj 在你的 Lua 代码基中的具体位置: https://github.com/openresty/nginx-gdb-utils#lgcpathlgcstat 工具的原理类似 GC 的 sweep 过程,而 lgcpath 的原理类似 GC 的 mark过程。最简单的,你在开发和测试环境使用最新版本的 OpenResty(目前是 1.9.7.4),尝试复现内存泄漏,然后直接使用上面的 gdb工具分析。On Tue, Dec 27, 2016 at 8:42 PM, 杨关道 <is0...@gmail.com> wrote:我的程序试图从后端服务器获取两组userid列表(userid之间用换行符分隔),然后将userid放到redis的set中程序运行逻辑没问题,但是发现运行后nginx worker的内存占用率很高代码:local iredis = require('ssp.models.iredis')local order_id = 18622local userlist_files = {"condition/uid/18622_1.txt", "condition/uid/18622_2.txt"} --每个txt文件大概5Mlocal user_lists={}for _, file in ipairs(userlist_files) do local res = ngx.location.capture('/backend', {method=ngx.HTTP_GET, vars={backend_host="http://d.ssp.xunlei.com", backend_uri="/public/upload/"..file}}) table.insert(user_lists, res.body)endlocal red = iredis:new()red:init_pipeline()local key = "ad:o:u:" .. order_idred:del(key)for _, userlist in ipairs(user_lists) do local it, err = ngx.re.gmatch(userlist, "[0-9]+") if not it then log(ngx.ERR, "gmatch err: ", err) else while true do local m, err = it() if err then log(ngx.ERR, err) break end if not m then -- no match found (any more) break end -- found a match red:sadd(key, m[0]) end endendred:expire(key, oneday)local ok, err = red:commit_pipeline()nginx 的/backend配置location /backend { internal; rewrite_by_lua 'ngx.req.set_uri(ngx.var.backend_uri)'; proxy_http_version 1.1; proxy_set_header Connection KeepAlive; proxy_set_header CLIENTADDR $remote_addr; proxy_set_header Content-Type application/x-www-form-urlencoded; proxy_pass $backend_host; }iredis其实是温总的那个redis二次封装的代码不知道哪里有问题,每次执行完之后,内存从50m跳到900m -- -- YuanSheng Wang---------------------------------------My Github: https://github.com/membphisOpenResty lover ^_^ --