数据库表如何释放内存?(100分)

  • 主题发起人 主题发起人 山水之间
  • 开始时间 开始时间

山水之间

Unregistered / Unconfirmed
GUEST, unregistred user!
我用access+ado开发一数据库处理数据。用timer出发,每个几秒读取数据,进行处理写入另一些数据表。但我发现timer触发的时间越来越长,从1秒到4秒到10秒,程序运行时间越长,程序占用的内存就越多。只有关闭程序重新启动才能解决问题。有没有办法在程序中释放打开多个数据表,画图等占用的内存,使程序运行过程中占用的内存保持相对稳定?
 
兄弟们,帮一把吧!
我每次打开adotable时,用完就close的。后来用free试了试,发现下次timer再触发时table一open就出错。是不是table不能用free啊?
 
free之后要再create
 
是不是所有的table,query都要动态创建才行啊?
 
timer这个控件慎用,呵呵
另外adotable不用去free,它是随着窗体的消亡而消亡。
你看看你的timer控件是不是和别的控件相关联而导致循环了???
 
to xieha:
我的timer 没有发生你所说的循环的事。如果不用timer,用什么东西来自动触发呢?(我要每隔几秒读一次数据,而且程序24×7小时运行)
 
适当掌握好timer触发的时间,应该没问题的,可能是你没有处理好,比如前一次被触发的事件
没完成或者没处理干净,后面不做相应判断又触发了它,这样后面的事件也会有“尾巴”,
如此连续触发,程序占用的内存就会越来越多。
 
我怀疑你的代码中有循环套用的问题,或者timer的ontimer事件中的代码有问题,能不能
把代码贴出来?[:)]
 
procedure Tmainfrm.Timer1Timer(Sender: TObject);
var 。。。。
begin
//先从数据表中读取数据:
adotable1:=Tadotable.create(self);
with adotable1 do
begin
connectionstring:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=newjg2';
tablename:='风口直径';
active:=true;
last;
for i:=1 to 14 do
zj:=fields[i+1].asfloat;
close;
adotable1.free;
end;



lbl1.caption:=inttostr(round(zj[1]));
lbl2.caption:=inttostr(round(zj[2]));
lbl3.caption:=inttostr(round(zj[3]));
lbl4.caption:=inttostr(round(zj[4]));
lbl5.caption:=inttostr(round(zj[5]));
lbl6.caption:=inttostr(round(zj[6]));
lbl7.caption:=inttostr(round(zj[7]));
lbl8.caption:=inttostr(round(zj[8]));
lbl9.caption:=inttostr(round(zj[9]));
lbl10.caption:=inttostr(round(zj[10]));
lbl11.caption:=inttostr(round(zj[11]));
lbl12.caption:=inttostr(round(zj[12]));
lbl13.caption:=inttostr(round(zj[13]));
lbl14.caption:=inttostr(round(zj[14]));
。。。

adotable1:=Tadotable.create(self);

with adotable1 do
begin
connectionstring:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=newjg2';
tablename:='yssj1';
active:=true;
last;
zgll[1]:=fieldbyname('fk1').asinteger;
zgll[2]:=fieldbyname('fk2').asinteger;
zgll[3]:=fieldbyname('fk3').asinteger;
zgll[4]:=fieldbyname('fk4').asinteger;
zgll[5]:=fieldbyname('fk5').asinteger;
zgll[6]:=fieldbyname('fk6').asinteger;
zgll[7]:=fieldbyname('fk7').asinteger;
zgll[8]:=fieldbyname('fk8').asinteger;
zgll[9]:=fieldbyname('fk9').asinteger;
zgll[10]:=fieldbyname('fk10').asinteger;
zgll[11]:=fieldbyname('fk11').asinteger;
zgll[12]:=fieldbyname('fk12').asinteger;
zgll[13]:=fieldbyname('fk13').asinteger;
zgll[14]:=fieldbyname('fk14').asinteger;
close;

adotable1.free;
end;


for i:=1 to 14 do
begin
if zgll<0 then zgll:=0;
end;

adotable1:=Tadotable.create(self);
with adotable1 do
begin
connectionstring:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=newjg2';
tablename:='yssj2';
active:=true;
last;
rfwd:=fieldbyname('rfwd').asinteger;
rfyl:=fieldbyname('rfyl').asinteger/10000;
lfll:=round(fieldbyname('lfll').asinteger);
lh[1]:=fieldbyname('lh1').asinteger;
lh[2]:=fieldbyname('lh2').asinteger;
lh[3]:=fieldbyname('lh3').asinteger;
lh[4]:=fieldbyname('lh4').asinteger;
lss[1]:=fieldbyname('lss1').asinteger;
lss[2]:=fieldbyname('lss2').asinteger;
lss[3]:=fieldbyname('lss3').asinteger;
lss[4]:=fieldbyname('lss4').asinteger;
lsx[1]:=fieldbyname('lsx1').asinteger;
lsx[2]:=fieldbyname('lsx2').asinteger;
lsx[3]:=fieldbyname('lsx3').asinteger;
lsx[4]:=fieldbyname('lsx4').asinteger;
close;

adotable1.free;
end;



//计算开始
。。。
//计算鼓风动能:
。。。
//在窗口中显示数据:
label17.caption:=inttostr(zgll[1]);
label18.caption:=inttostr(zgll[2]);
label19.caption:=inttostr(zgll[3]);
label20.caption:=inttostr(zgll[4]);
label21.caption:=inttostr(zgll[5]);
label22.caption:=inttostr(zgll[6]);
label23.caption:=inttostr(zgll[7]);
label24.caption:=inttostr(zgll[8]);
label25.caption:=inttostr(zgll[9]);
label26.caption:=inttostr(zgll[10]);
label27.caption:=inttostr(zgll[11]);
label28.caption:=inttostr(zgll[12]);
label29.caption:=inttostr(zgll[13]);
label30.caption:=inttostr(zgll[14]);
。。。
//写入瞬时数据库 (180条记录,保证数据曲线输出清晰)

adotable1:=Tadotable.create(self);
with adotable1 do
begin
connectionstring:='Provider=MSDASQL.1;Persist Security Info=False;Data Source=newjg2';
tablename:='支管流量';
if not active then active:=true;
if recordcount>=180 then
begin
first;
edit;
delete;
last;
edit;
append;
end
else
begin
last;
edit;
append;
end;
FieldValues['dt'] := now;
dtnow:=now;
FieldValues['fk1'] :=zgll[1];
FieldValues['fk2'] :=zgll[2];
FieldValues['fk3'] :=zgll[3];
FieldValues['fk4'] :=zgll[4];
FieldValues['fk5'] :=zgll[5];
FieldValues['fk6'] :=zgll[6];
FieldValues['fk7'] :=zgll[7];
FieldValues['fk8'] :=zgll[8];
FieldValues['fk9'] :=zgll[9];
FieldValues['fk10'] :=zgll[10];
FieldValues['fk11'] :=zgll[11];
FieldValues['fk12'] :=zgll[12];
FieldValues['fk13'] :=zgll[13];
FieldValues['fk14'] :=zgll[14];
fieldvalues['lfll'] :=lfll;
FieldValues['pjz'] :=avg;
FieldValues['dm'] :=dt+tm/1000000; ;
Post;
i:=1;
first;
while not eof do
begin
zg1:=fieldbyname('fk1').asinteger;
zg2:=fieldbyname('fk2').asinteger;
zg3:=fieldbyname('fk3').asinteger;
zg4:=fieldbyname('fk4').asinteger;
zg5:=fieldbyname('fk5').asinteger;
zg6:=fieldbyname('fk6').asinteger;
zg7:=fieldbyname('fk7').asinteger;
zg8:=fieldbyname('fk8').asinteger;
zg9:=fieldbyname('fk9').asinteger;
zg10:=fieldbyname('fk10').asinteger;
zg11:=fieldbyname('fk11').asinteger;
zg12:=fieldbyname('fk12').asinteger;
zg13:=fieldbyname('fk13').asinteger;
zg14:=fieldbyname('fk14').asinteger;
zgpj:=fieldbyname('pjz').asinteger;
shij:=fieldbyname('dm').asfloat;
next;
i:=i+1;
end;
close;
adotable1.free;
end;

//判断是否该统计5分钟的数据
if (((min+1) mod 5)=0) and (sec>=50)then
begin
//支管流量表
for i:=1 to 14 do htotal:=0;
lftotal:=0;
count:=0;
with datamodule1.zglltb do
begin
if active=false then active:= true;
last;
while not bof do
begin
if ((SecondDiff(now,fieldbyname('dt').asdatetime)<300)) and
((secondDiff(now,fieldbyname('dt').asdatetime))>=0) then
begin
for i:=1 to 14 do
htotal:=htotal+fields[i+1].asinteger;
lftotal:=lftotal+fields[16].asinteger;
count:=count+1;
prior;
end
else
break;
end;
end;
//将平均数据写入数据库
也采用动态创建adotable并free;
在本窗口中画柱状图
//曲线显示
with rsdchart do
begin
with series1 do
begin
clear;
add(gfdn[1],'1');
add(gfdn[2],'2');
add(gfdn[3],'3');
add(gfdn[4],'4');
add(gfdn[5],'5');
add(gfdn[6],'6');
add(gfdn[7],'7');
add(gfdn[8],'8');
add(gfdn[9],'9');
add(gfdn[10],'10');
add(gfdn[11],'11');
add(gfdn[12],'12');
add(gfdn[13],'13');
add(gfdn[14],'14');
end;
with series2 do
begin
clear;
add(lastgfdn[1],'1');
add(lastgfdn[2],'2');
add(lastgfdn[3],'3');
add(lastgfdn[4],'4');
add(lastgfdn[5],'5');
add(lastgfdn[6],'6');
add(lastgfdn[7],'7');
add(lastgfdn[8],'8');
add(lastgfdn[9],'9');
add(lastgfdn[10],'10');
add(lastgfdn[11],'11');
add(lastgfdn[12],'12');
add(lastgfdn[13],'13');
add(lastgfdn[14],'14');
end;
end;
//在另一窗体中作图:
//燃烧带动态显示
a:=400;
b:=210;
d:=320;
r1:=8;
r2:=11;
r3:=14;
r4:=16;
r5:=18;
r6:=18;
r7:=18;
r8:=18;
dis1:=5;
dis:=10;

x1:=round(a-d);
y1:=round(b-d/2);
x2:=round(a);
y2:=round(b+d/2);
circlefrm.canvas.brush.color:=clgray;
circlefrm.canvas.ellipse(x1-25,y1-25,x2+25,y2+25);

circlefrm.canvas.brush.color:=clred;
circlefrm.canvas.ellipse(x1,y1,x2,y2);


//dis 为控制长度参数
for i:=1 to 14 do gfdntt:=gfdntt+gfdn;
gfdntt:=gfdntt/14;
for i:=1 to 14 do change:=10*(gfdn-gfdntt)/(gfdntt);
for i:=1 to 14 do ddd:=sqrt(gfdn)/6;
agl:=0;
if gfdn[11]<>0 then
begin
dis2:=ddd[11];
circlefrm.drawelps;
end;
agl:=25.7;
if gfdn[10]<>0 then
begin
dis2:=ddd[10];
circlefrm.drawelps;
end;
agl:=51.4;
if gfdn[9]<>0 then
begin
dis2:=ddd[9];
circlefrm.drawelps;
end;
agl:=77.1;
if gfdn[8]<>0 then
begin
dis2:=ddd[8];
circlefrm.drawelps;
end;
agl:=102.8;
if gfdn[7]<>0 then
begin
dis2:=ddd[7];
circlefrm.drawelps;
end;
agl:=128.5;
if gfdn[6]<>0 then
begin
dis2:=ddd[6];
circlefrm.drawelps;
end;
agl:=154.2;
if gfdn[5]<>0 then
begin
dis2:=ddd[5];
circlefrm.drawelps;
end;
agl:=180;
if gfdn[4]<>0 then
begin
dis2:=ddd[4];
circlefrm.drawelps;
end;
agl:=205.6;
if gfdn[3]<>0 then
begin
dis2:=ddd[3];
circlefrm.drawelps;
end;
agl:=231.3;
if gfdn[2]<>0 then
begin
dis2:=ddd[2];
circlefrm.drawelps;
end;
agl:=257;
if gfdn[1]<>0 then
begin
dis2:=ddd[1];
circlefrm.drawelps;
end;
agl:=282.7;
if gfdn[14]<>0 then
begin
dis2:=ddd[14];
circlefrm.drawelps;
end;
agl:=308.4;
if gfdn[13]<>0 then
begin
dis2:=ddd[13];
circlefrm.drawelps;
end;
agl:=334.1;
if gfdn[12]<>0 then
begin
dis2:=ddd[12];
circlefrm.drawelps;
end;
flag1:=false;
for i:=1 to 14 do
if lastzgll>0 then flag1:=true;
with circlefrm do
begin
with rsdchart do
begin
with series1 do
begin
clear;
add(zgll[1],'1');
add(zgll[2],'2');
add(zgll[3],'3');
add(zgll[4],'4');
add(zgll[5],'5');
add(zgll[6],'6');
add(zgll[7],'7');
add(zgll[8],'8');
add(zgll[9],'9');
add(zgll[10],'10');
add(zgll[11],'11');
add(zgll[12],'12');
add(zgll[13],'13');
add(zgll[14],'14');
end;
with series2 do
begin
clear;
add(lastzgll[1],'1');
add(lastzgll[2],'2');
add(lastzgll[3],'3');
add(lastzgll[4],'4');
add(lastzgll[5],'5');
add(lastzgll[6],'6');
add(lastzgll[7],'7');
add(lastzgll[8],'8');
add(lastzgll[9],'9');
add(lastzgll[10],'10');
add(lastzgll[11],'11');
add(lastzgll[12],'12');
add(lastzgll[13],'13');
add(lastzgll[14],'14');
end;
end;
end;
在窗体3中画瞬时曲线
with fkfrm do
begin
for i:=1 to 180 do
begin
series1.AddXY(shij,zg1,'',);
series2.AddXY(shij,zg2,'',);
series3.AddXY(shij,zg3,'',);
series4.AddXY(shij,zg4,'',);
series5.AddXY(shij,zg5,'',);
series6.AddXY(shij,zg6,'',);
series7.AddXY(shij,zg7,'',);
series8.AddXY(shij,zg8,'',);
series9.AddXY(shij,zg9,'',);
series10.AddXY(shij,zg10,'',);
series11.AddXY(shij,zg11,'',);
series12.AddXY(shij,zg12,'',);
series13.AddXY(shij,zg13,'',);
series14.AddXY(shij,zg14,'',);
series15.AddXY(shij,zgpj,'',);


end;
end;

虽然用了free释放adotable占用的内存,但程序的内存还是不断在增加,
是否还有些占用资源的语句和控件?肯请各位大师指正!!!


 
我觉得你用动态创建好,
因为我感觉动态创建的速度要比控件快很多
先Create
后Free
 
是不是使用数组也在占用内存啊??
 
你似乎已经找到了解决问题的答案 [:D]
 
我做了测试,我觉得数组的问题不是太大,主要还是以下的chart中作图吃的内存特别明显。程序每10秒触发一次,就画15组曲线,过20分钟后,占的内存是初始的2倍。我如果用动态创建series的话,如果free一下,就无法显示出曲线了,这如何解决,恳请指导!!
series1.AddXY(shij,zg1,'',);
series2.AddXY(shij,zg2,'',);
。。。
 
用chart1.freeallseries
 
后退
顶部