开发者

Do variables that reference an object that is stored in a Session variable get collect by GC?

I'm not all that familiar with how garbage collection works and what causes memory leaks. But I'm at the point were I'm becoming concerned about it and want to write more efficient code. So the project I'm working on, which is an Asp.Net web application, has an instance of a custom DataCriteria being created when a Session starts in the global.asax and is then stored in a Session variable. This custom DataCriteria is what we are using to communicate with the database for the CRUD methods.

First question is, say we have a Person class, and in this Person class there is a DataCriteria field which is set to the Session variable instance of the DataCriteria. Since an instance of Person holds a reference 开发者_StackOverflowto an instance of the DataCriteria that will not get disposed of till Session ends, will that instance of Person be able to be collected. Or will every instance of Person not be disposed of till that Session ends.

Second question is more general, but same issue. Basically I'm wondering will a variable that is declared in a method that references the Session variable instance of the DataCriteria be able to be collected by the GC? Or will it too stay till the Session ends?

I thank you for all the advise you can give me here. And if you know of any good reads for understanding Garbage Collection and avoiding memory leaks I would love to hear about them too.


The references held by an object do not affect its availability for garbage collection. However, the inverse is not true. For example, let's say an instance A has a field that holds an instance B. A will become available for garbage collection as soon as nothing else references it, even if B is static or otherwise "long lived". However, B will not become available for garbage collection at least until A becomes available for garbage collection.

Where things can get a bit odd is where references are not too obvious. For example, an instance C becomes referenced by an instance D when C subscribes to an event exposed by D. This means that, if D is long-lived, C will not become available for garbage collection before D unless it unsubscribes from the event. References held via events and other delegates actually account for most of the "memory" leaks in .NET applications. (Properly speaking, these aren't really memory leaks since there is no actual failure to clean up memory that is not used by actual object instances.)


Object references constitute a directed graph - object A references object B. Until A is collected, B cannot be.

Session is just another object that references stuff - it is object A referencing your object B in this case.

So in your case, just becuase Person references something also stored in Session does not mean Person stays around until Session goes away. unless Session references your Person, their lifetimes are not linked.

Now what you DO have to watch out for is what else your DataCriteria references. If it holds database connections or other objects, they will live as long as the Session does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜