| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 701 人关注过本帖
标题:内存问题~~
取消只看楼主 加入收藏
初学Delphi
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:330
专家分:0
注 册:2006-4-4
收藏
 问题点数:0 回复次数:4 
内存问题~~


这个是写的通信的,执行过程中到第317条记录的时候就提示内存不足了,

除了下面我用到的方法,还要怎么释放内存啊?

private void button4_Click(object sender, EventArgs e)
{
string ip = "192.168.1.5";
string port = "8686";
IPAddress serverIp = IPAddress.Parse(ip);
int serverPort = Convert.ToInt32(port);
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
SqlCeConnection conn = new SqlCeConnection(@"Data Source = canyin111.sdf");
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("delete from beizhu",conn);
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
conn.Dispose();
n = 0;
beizhushu = 0;
shuaxinshuzu();
sendMessage[0] = 0x1d;
sendMessage[1] = 0x1b;
sendMessage[2] = 0x00;
sendMessage[3] = 0x38;
sendMessage[4] = 0x00;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(iep);
socket.Send(sendMessage, 5, 0);
socket.Receive(byteMessage);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
if (byteMessage[1] == 0x02)
{
beizhushu = Convert.ToInt32(byteMessage[3] * 256 + byteMessage[4]);
if(beizhushu>0)
{
shuaxinshuzu();
sendMessage[0] = 0x1f;
sendMessage[1] = 0x02;
sendMessage[2] = 0x1b;
sendMessage[3] = 0x00;
sendMessage[4] = 0x3c;
sendMessage[5] = 0x00;
Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket1.Connect(iep);
socket1.Send(sendMessage, 6, 0);
socket1.Receive(byteMessage);
socket1.Shutdown(SocketShutdown.Both);
socket1.Close();
beizhugengxin();
}
else
{
MessageBox.Show("下载完毕!");
}
}
GC.Collect();
}
private void beizhugengxin()
{
int k = 1;
int changdu = 0;
while (byteMessage[k] != 0x1b)
{
changdu++;
k++;
}
string bianhao = Encoding.Default.GetString(byteMessage, k - changdu, changdu);
k++;
changdu = 0;
while (byteMessage[k] != 0x1b)
{
k++;
changdu++;
}
string beizhu = Encoding.Default.GetString(byteMessage, k - changdu, changdu);

try
{
SqlCeConnection conn = new SqlCeConnection(@"Data Source = canyin111.sdf");
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("insert into beizhu values('" + bianhao + "','" + beizhu + "')", conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Dispose();
conn.Close();
n++;
if (n < beizhushu)
{
//if(n%100==0)
//{
// MessageBox.Show(n.ToString());
//}
shuaxinshuzu();
//循环上传结果
sendMessage[0] = 0x21;
sendMessage[1] = 0x02;
sendMessage[2] = 0x1b;
sendMessage[3] = Convert.ToByte(n / 256);
sendMessage[4] = Convert.ToByte(n % 256);
sendMessage[5] = 0x1b;
ushort jiaoyan = 0;
for (int ii = 0; ii < 6; ii++)
{
jiaoyan += sendMessage[ii];
}
sendMessage[6] = Convert.ToByte(jiaoyan / 256);
sendMessage[7] = Convert.ToByte(jiaoyan % 256);
sendMessage[8] = 0x00;
string ip = "192.168.1.5";
string port = "8686";
IPAddress serverIp = IPAddress.Parse(ip);
int serverPort = Convert.ToInt32(port);
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(iep);
socket.Send(sendMessage, 9, 0);
socket.Receive(byteMessage);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
GC.Collect();
beizhugengxin();

}
else if (n == beizhushu)
{
MessageBox.Show("下载完毕!");
}
}
catch
{
//MessageBox.Show(ee.Message);
SqlCeConnection conn = new SqlCeConnection(@"Data Source = canyin111.sdf");
conn.Open();
SqlCeCommand cmd = new SqlCeCommand("select count(*) from beizhu", conn);
SqlCeDataReader dr= cmd.ExecuteReader();
string aaa=dr[0].ToString();
MessageBox.Show(aaa);

}
}

搜索更多相关主题的帖子: 内存 
2006-12-01 15:09
初学Delphi
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:330
专家分:0
注 册:2006-4-4
收藏
得分:0 
以下是引用IT浪子在2006-12-1 18:14:08的发言:
回楼上你的电脑配置咋样呀,不是这个的问题吧,

我用的是虚拟机,程序分配的内存是15M,程序运行起来还剩6兆多,但一执行上面的程序就一点点的减少,传317还是371条的时候就内存不足了,是不是那些全局变量也占内存?怎么释放啊?

2006-12-02 08:04
初学Delphi
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:330
专家分:0
注 册:2006-4-4
收藏
得分:0 
还有是不是跟我这调用方法有关系?一直调用他自身程序,直到条件(n=beizhushu)
2006-12-02 08:06
初学Delphi
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:330
专家分:0
注 册:2006-4-4
收藏
得分:0 

那个 private void beizhugengxin()方法执行一次内存就少了1兆多,然后gc.collect就会释放刚才占用的90%多,差不多100条就占用1M的内存了

2006-12-02 09:04
初学Delphi
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:330
专家分:0
注 册:2006-4-4
收藏
得分:0 

我就是想问一下哪些是没用完的资源,该怎么解决。

我这里面好像就是公共变量是没有使用完的资源

另外我把socket设成了公共变量了,不是每次创建一个对象(方法结束的时候gc.collect一下)。

这两种哪个更节省资源?

2006-12-02 11:27
快速回复:内存问题~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.075805 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved