咨询一个关于js中内存泄漏的问题
发布于 1 个月前 作者 DerekYeung 300 次浏览 来自 问答

我目前在使用Koa 1.0,自己做了一些简单的封装,最近项目上线后发现内存一直在上升,没有下降,怀疑有内存泄漏,随之进行了一次排查,经过反复的断点测试之后目前初步怀疑是数据库模块产生的问题,最后定位到这段代码

ActiveQuery.prototype.afterQueryRecord = function(datas, one){
	var self = this;
	if( this.AR.related && datas.length > 0 ){
		console.log('beginning search related : ' + self.id);
		for( k in self.AR.related ){
			var query = self.AR.related[k];
			if( query.relate_status && query.relate_status == 'pending' ){
				self.pending.push(k);
			}
		}
		if( self.pending.length == 0 ){
			return self.getQueryRecord(datas, one)
		}
		return self.findRelationData(datas, one);
	}else{
		return this.getQueryRecord(datas, one);
	}
};
ActiveQuery.prototype.findRelationData = function(datas, one){
	var self = this;
	return new Promise(function(resolve, reject){
		if( self.pending.length <= 0 ){
			resolve(self.getQueryRecord(datas, one));
		}else{
			var name = self.pending[0];
			var related = self.AR.related[name];
			related.query.findRelation(datas).then(function(rows){
				console.log('finded relation ' + related.name);
				self.AR._related[name] = rows;
				self.pending.splice(0, 1);
				resolve(self.findRelationData(datas, one));
			});
		}
	});
};
ActiveQuery.prototype.getQueryRecord = function(datas, one){
	if( one ){
		datas = datas[0] ? datas[0] : {};
	}
	var attributes = clone(datas);

	function QueryRecord(){
	};
	QueryRecord.prototype = clone(Object.getPrototypeOf(this.AR));
	QueryRecord.prototype._attributes = attributes;
	if( this.options.security ){
		datas = this.securityDatas(datas);
	}
	QueryRecord.prototype._datas = datas;
	QueryRecord.prototype.setting = this.AR.setting;
	QueryRecord.prototype.tableName = this.AR.tableName;
	QueryRecord.prototype.one = one;
	QueryRecord.prototype.isSecurity = this.options.security ? 1 : 0;
	QueryRecord.prototype.toString = function(){
		return '[QueryRecord Object]';
	};
	var record = new QueryRecord();
	if( one ){
		if( datas ){
			for( k in datas ){
				record[k] = datas[k];
			}
		}
	}else{
		for( var i = 0; i < datas.length; i++ ){
			record[i] = datas[i];
		}
	}
	var result = record;
	if( this.options.asArray ){
		result = record.parsed().toArray();
	}
	return result;
	// return this.options.asArray ? record.toArray() : record;
};

self.AR.related里有预设好的其他表的类 这段代码是在执行完mysql query后根据related去查找关联关系,然后绑定到当前的类中,想咨询一下各位大神请问这样的话会不会导致内存泄漏,原理是什么,非常感谢

回到顶部