从另一个对象内部返回对象时的内存问题 [英] Memory question when returning an object from inside another object

查看:48
本文介绍了从另一个对象内部返回对象时的内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




问题快速概述:


public bool Something(out DataSet ds)

{

bool ret = false;


尝试

{

xmlDocument myXmlDoc = new xmlDocument (myFile.xml);


ds = myXmlDoc.DataSet;


ret = true;

} < br $>
catch(例外e)

{

//做点什么

}


返回ret;

}


如何释放xmlDocument的内存?调用此函数的对象

会有一个引用(如果我错了就纠正我)

到xmlDocument里面的DataSet,那么c#内存管理器也是如此
忽略这个对象?


我想更换:


ds = myXmlDoc.DataSet;

with:


ds = myXmlDoc.DataSet.Copy();

myXmlDoc = null; //这会让内存被释放吗?


任何对此的帮助都会受到赞赏。


问候,


史蒂文



***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

Hi,

Quick overview of the problem:

public bool Something( out DataSet ds )
{
bool ret=false;

try
{
xmlDocument myXmlDoc = new xmlDocument( myFile.xml );

ds = myXmlDoc.DataSet;

ret=true;
}
catch( Exception e )
{
//Do something
}

return ret;
}

How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?

I thought about replacing:

ds = myXmlDoc.DataSet;

with:

ds = myXmlDoc.DataSet.Copy();
myXmlDoc = null; //Would this allow memory to be released?

Any help on this would be appreciated.

Regards,

Steven



*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

我肯定Jon Skeet会在观看并添加他的两个便士价值(比我的价值多b
)意见被重视!)但我觉得它类似于

这个:


myXmlDoc是一个引用XmlDocument对象的变量。然后,这个

XmlDocument对象由DataSet对象引用。因此,

对象仅被清理。当垃圾收集器对象没有

更长时间被引用此XmlDocument变量或DataSet

对象...你不需要调用副本( )方法,除非你想要一个

不同的对象,从原来的XmlDocument复制。


我相信你也可以这样做:

public bool Something(Dataset ds)

,它传入对数据集对象的引用。因此对此

对象的更改将反映在原始对象中。


Steven Blair < ST ********** @ btinternet.com>在消息中写道

新闻:OE ************** @ TK2MSFTNGP11.phx.gbl ...
I''m sure Jon Skeet will be watching and add his two pennies worth (which is
more than my opinions are valued at!) but I think it goes something like
this:

myXmlDoc is a variable that references an XmlDocument object. This
XmlDocument object is then referenced by a DataSet object. Therefore the
object is only "cleaned up" by the garbage collector when the object is no
longer being referenced either this XmlDocument variable or the DataSet
object... You don''t need to call the Copy() method unless you want a
different object, copied from your original XmlDocument.

I do believe you can also do this:
public bool Something ( Dataset ds )
which passes in a reference to the dataset object. Therefore changes to this
object are reflected in the original.

"Steven Blair" <st**********@btinternet.com> wrote in message
news:OE**************@TK2MSFTNGP11.phx.gbl...

快速概述问题:

public bool Something(out DataSet ds)
{
bool ret = false;

尝试
{xmlDocument myXmlDoc = new xmlDocument(myFile.xml);

ds = myXmlDoc.DataSet;

ret = true;
}
catch(例外e)
{
//做点什么
}
返回ret;
}

如何释放xmlDocument的内存?调用此函数的对象将有一个引用(如果我错了,请更正我)
到xmlDocument中的DataSet,那么c#内存管理器会忽略这个对象吗?

我想要替换:

ds = myXmlDoc.DataSet;

用:

ds = myXmlDoc.DataSet.Copy() ;
myXmlDoc = null; //这会让内存被释放吗?

对此有任何帮助将不胜感激。

问候,

史蒂文
<

***通过开发人员指南发送 http://www.developersdex .com ***
不要只是参加USENET ......获得奖励!
Hi,

Quick overview of the problem:

public bool Something( out DataSet ds )
{
bool ret=false;

try
{
xmlDocument myXmlDoc = new xmlDocument( myFile.xml );

ds = myXmlDoc.DataSet;

ret=true;
}
catch( Exception e )
{
//Do something
}

return ret;
}

How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?

I thought about replacing:

ds = myXmlDoc.DataSet;

with:

ds = myXmlDoc.DataSet.Copy();
myXmlDoc = null; //Would this allow memory to be released?

Any help on this would be appreciated.

Regards,

Steven



*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



我猜你的意思是XmlDataDocument,而不是XmlDocument?


我相信如果你将
直接为你的out分配XmlDataDocument.DataSet属性,那么XmlDataDocument对于GC是不可思议的br />
参数。原因是XmlDataDocument使用DataSet注册事件

处理程序方法,以便能够在DataSet中发生任何更改时更新

文档。这意味着

DataSet实际上有一个参考(实际上好几个)到

XmlDataDocument。


除此之外,以下代码无效。 XmlDataDocument类确实没有公开任何获取XML文档路径的构造函数,并且

XmlDocument类不公开DatSet属性。


这真的是你的副本吗?


/ Joakim


Steven Blair写道:
I guess you mean XmlDataDocument, not XmlDocument?

I believe the XmlDataDocument will not be eligeble for GC if you
directly assign the XmlDataDocument.DataSet property to your out
parameter. The reason is that the XmlDataDocument registers event
handler methods with the DataSet in order to be able to update the
document as soon anything changes in the DataSet. This means that the
DataSet in fact has a reference (well several actually) to the
XmlDataDocument.

That aside, the code below will not work. The XmlDataDocument class does
not expose any constructor that takes a path to an XML-document, and the
XmlDocument class does not expose a DatSet property.

Is this really your repro case?

/Joakim

Steven Blair wrote:


快速概述问题:

公共bool Something(out DataSet ds)
{
bool ret = false;

尝试
{x / dmlment myXmlDoc = new xmlDocument(myFile.xml);

ds = myXmlDoc.DataSet;

= true;
}
catch(例外e)
{
//做点什么


返回ret;
}

如何释放xmlDocument的内存?调用此函数的对象将有一个引用(如果我错了,请更正我)
到xmlDocument中的DataSet,那么c#内存管理器会忽略这个对象吗?

我想要替换:

ds = myXmlDoc.DataSet;

用:

ds = myXmlDoc.DataSet.Copy() ;
myXmlDoc = null; //这会让内存被释放吗?

对此有任何帮助将不胜感激。

问候,

史蒂文
<

***通过开发人员指南发送 http://www.developersdex .com ***
不要只是参加USENET ......获得奖励!
Hi,

Quick overview of the problem:

public bool Something( out DataSet ds )
{
bool ret=false;

try
{
xmlDocument myXmlDoc = new xmlDocument( myFile.xml );

ds = myXmlDoc.DataSet;

ret=true;
}
catch( Exception e )
{
//Do something
}

return ret;
}

How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?

I thought about replacing:

ds = myXmlDoc.DataSet;

with:

ds = myXmlDoc.DataSet.Copy();
myXmlDoc = null; //Would this allow memory to be released?

Any help on this would be appreciated.

Regards,

Steven



*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



谢谢花时间回复。


之所以有一些拼写错误的原因是源代码现在不能用于
我(它坐在另一台PC上)这是锁定atm)


是的,它是一个XmlDataDocument ...


我不能直接记住代码,但是我加载了一个将xml文件放入此

对象中。它可能有一个LoadXML mehtod。


所以当我调用ds = myXMLDoc.DataSet时,这会阻止XmlDataDocument

内存被释放......我怎么样?确保这个对象的内存释放了吗?


另一个快速的问题:


ds = myXMLDoc。 DataSet;


这会创建一个新的DataSet,还是ds是对XMLDataDocument中保存的

DataSet的引用?

问候,


史蒂文


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ...奖励它!
Thanks for taking the time to reply.

The reason why there is a few typos is the source code isnt available to
me right now (its sitting on another PC which is locked atm)

Yes its a XmlDataDocument...

I can''t remember the code directly, but I load a xml file into this
object. It might have a LoadXML mehtod.

So when I call ds = myXMLDoc.DataSet this prevents the XmlDataDocument
memory from being released... How would I ensure that the memory for
this object is released?

Another quick question:

ds = myXMLDoc.DataSet;

Would this create a new DataSet, or would ds be a reference to the
DataSet held inside the XMLDataDocument?

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于从另一个对象内部返回对象时的内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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