简单问题:为什么这会导致大量内存泄漏? [英] Simple problem: Why does this cause a massive memory leak?

查看:63
本文介绍了简单问题:为什么这会导致大量内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的内存泄漏问题真的非常简单。

插入代码。

在我的应用程序中,我正在尝试使用类型化数据集插入数据库。

它添加了很多行(比如数百个)。不幸的是它有一个很大的屁股

内存泄漏,我不明白为什么。


下面的代码演示了内存泄漏。在调用PerformOperation之后,就好像什么都没有。



根据.net内存分析器,在运行控制台应用程序后,有

是大量未经处理的对象,而且使用的是meory用途。


我试图附上来自.net内存分析器的截图显示什么

我的意思是


非常感谢任何帮助


谢谢


Simon


class Program {


static void Main(string [] args){

for( int i = 0; i< 1000; i ++){

PerformOperation();

}

}

private static void PerformOperation(){

GPSPositionTableAdapter da = new GPSPositionTableAdapter();

GPSPositionTable tblGPSPositions = new GPSPositionTable();

GPSPositionRow currentRow = null;


for(int i = 0; i< 50; i ++){


currentRow = tb lGPSPositions.NewGPSPositionRow();


currentRow.DownloadID = 1;

currentRow.TTUReference = 9999;

currentRow.Timestamp = DateTime.Now;

currentRow.Latitude = 66.66666666666;

currentRow.Longitude = 66.66666666666;


tblGPSPositions.AddGPSPositionRow(currentRow) ;

}


da.Update(tblGPSPositions);

}


}

Hi all,

I have a huge memory leak problem in what is really very simple data
insert code.

In my app I''m trying to use a typed dataset to insert into a database.
It adds quite a few rows (say hundreds). Unfortunately it has a big ass
memory leak and I dont understand why.

The code below demonstrates the memory leak. It''s like nothing is
cleaned up after PerformOperation is called.

According to .net memory profiler, after running the console app, there
are huge numbers of undisposed objects and meory usage is through the roof.

I''ve tried to attach a screenshot from .net memory profiler showing what
I mean

Any help would be very much appreciated

Thanks

Simon

class Program {

static void Main(string[] args) {
for (int i = 0; i < 1000; i++) {
PerformOperation();
}
}

private static void PerformOperation() {
GPSPositionTableAdapter da = new GPSPositionTableAdapter();
GPSPositionTable tblGPSPositions = new GPSPositionTable();
GPSPositionRow currentRow = null;

for (int i = 0; i < 50; i++) {

currentRow = tblGPSPositions.NewGPSPositionRow();

currentRow.DownloadID = 1;
currentRow.TTUReference = 9999;
currentRow.Timestamp = DateTime.Now;
currentRow.Latitude = 66.66666666666;
currentRow.Longitude = 66.66666666666;

tblGPSPositions.AddGPSPositionRow(currentRow);
}

da.Update(tblGPSPositions);
}

}

推荐答案

Simon,


我很好奇,为什么你认为应该处置物品? GC

不是确定性的,你不能通过查看程序我应该

在这里处置了对象来说。唯一可以开始制作

假设的地方是你在GC类上明确调用静态Collect

方法,你不这样做在这里。


你有什么想做的?

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Simon" < si *** @ nothanks.com写信息

新闻:uq ************** @ TK2MSFTNGP03.phx.gbl ...
Simon,

I am curious, why do you think there should be disposed objects? The GC
is not deterministic, and you cant say by looking at a program "I should
have disposed objects here". The only place you can even begin to make that
sort of assumption is where you have an explicit call to the static Collect
method on the GC class, which you do not have here.

What is it that you are looking to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Simon" <si***@nothanks.comwrote in message
news:uq**************@TK2MSFTNGP03.phx.gbl...

大家好,


我真的非常简单的数据中存在巨大的内存泄漏问题

insert代码。


在我的应用程序中,我正在尝试使用类型化数据集插入数据库。它

增加了不少行(比如几百行)。不幸的是它有一个很大的屁股

内存泄漏,我不明白为什么。


下面的代码演示了内存泄漏。在调用PerformOperation之后,就好像没有任何东西被清理了




根据.net内存分析器,在运行控制台应用程序后,有

是大量未经处理的物品,而且使用的是通过

屋顶。


我试图附上一个截图.net内存分析器显示我

是什么意思


任何帮助都将非常感谢


谢谢


Simon


class Program {


static void Main(string [] args){

for(int i = 0; i< 1000; i ++){

PerformOperation();

}

} $ / $

private static void PerformOperation(){

GPSPositionTableAdapter da = new GPSPositionTableAdapter();

GPSPositionTable tblGPSPositions = new GPSPositionTable( );

GPSPositionRow currentRow = null;


for(int i = 0; i< 50; i ++){

currentRow = tblGPSPositions.NewGPSPositionRow();


currentRow.DownloadID = 1;

currentRow.TTUReference = 9999;

currentRow.Timestamp = DateTime.Now;

currentRow.Latitude = 66.66666666666;

currentRow.Longitude = 66.66666666666;


tblGPSPositions.AddGPSPositionRow(currentRow);

}


da.Update(tblGPSPositions);

}


}
Hi all,

I have a huge memory leak problem in what is really very simple data
insert code.

In my app I''m trying to use a typed dataset to insert into a database. It
adds quite a few rows (say hundreds). Unfortunately it has a big ass
memory leak and I dont understand why.

The code below demonstrates the memory leak. It''s like nothing is cleaned
up after PerformOperation is called.

According to .net memory profiler, after running the console app, there
are huge numbers of undisposed objects and meory usage is through the
roof.

I''ve tried to attach a screenshot from .net memory profiler showing what I
mean

Any help would be very much appreciated

Thanks

Simon

class Program {

static void Main(string[] args) {
for (int i = 0; i < 1000; i++) {
PerformOperation();
}
}

private static void PerformOperation() {
GPSPositionTableAdapter da = new GPSPositionTableAdapter();
GPSPositionTable tblGPSPositions = new GPSPositionTable();
GPSPositionRow currentRow = null;

for (int i = 0; i < 50; i++) {

currentRow = tblGPSPositions.NewGPSPositionRow();

currentRow.DownloadID = 1;
currentRow.TTUReference = 9999;
currentRow.Timestamp = DateTime.Now;
currentRow.Latitude = 66.66666666666;
currentRow.Longitude = 66.66666666666;

tblGPSPositions.AddGPSPositionRow(currentRow);
}

da.Update(tblGPSPositions);
}

}



根据.net内存分析器,运行控制台应用程序后,有
According to .net memory profiler, after running the console app, there

是大量未使用的对象,而使用的是使用

屋顶。
are huge numbers of undisposed objects and meory usage is through the
roof.



如果你想要处理对象,而不是在

集合期间完成,你必须在每个对象上调用IDisposable.Dispose。在大多数情况下,使用

子句是一种方便的方法。

If you want objects to be disposed, as opposed to finalized during
collection, you must call IDisposable.Dispose on each object. The using
clause is a convenient way to do this in most cases.


simon,


见内联
simon,

See inline

private static void PerformOperation(){

GPSPositionTableAdapter da = new GPSPositionTableAdapter();

GPSPositionTable tblGPSPositions = new GPSPositionTable();
private static void PerformOperation() {
GPSPositionTableAdapter da = new GPSPositionTableAdapter();
GPSPositionTable tblGPSPositions = new GPSPositionTable();



这是旧的tblGSSPostitions应该给予GC的地方

,因为没有任何行连接到它实例由

currentRow添加到tblGPSPpostitions。


也许在新的帮助之前可以简单明了。

Cor

This is te place where the old tblGSSPostitions should be given to the GC as
long as there are no rows anymore to it attached by instance by the
currentRow which is added to the tblGPSPpostitions.

Maybe will a simple clear before the new help in this.

Cor


GPSPositionRow currentRow = null;


for(int i = 0; i< 50; i ++){


currentRow = tblGPSPositions.NewGPSPositionRow();


currentRow.DownloadID = 1;

currentRow.TTUReference = 9999;

currentRow.Timestamp = DateTime.Now;

currentRow.Latitude = 66.66666666666;

currentRow。经度= 66.66666666666;


tblGPSPositions.AddGPSPositionRow(currentRow);

}


da.Update(tblGPSPositions) ;

}


}
GPSPositionRow currentRow = null;

for (int i = 0; i < 50; i++) {

currentRow = tblGPSPositions.NewGPSPositionRow();

currentRow.DownloadID = 1;
currentRow.TTUReference = 9999;
currentRow.Timestamp = DateTime.Now;
currentRow.Latitude = 66.66666666666;
currentRow.Longitude = 66.66666666666;

tblGPSPositions.AddGPSPositionRow(currentRow);
}

da.Update(tblGPSPositions);
}

}



这篇关于简单问题:为什么这会导致大量内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆