Click here to Skip to main content
16,004,919 members
Home / Discussions / C#
   

C#

 
GeneralRe: Dispose Method Pin
Luc Pattyn30-Jul-07 4:50
sitebuilderLuc Pattyn30-Jul-07 4:50 
GeneralRe: Dispose Method [modified] Pin
Martin#30-Jul-07 5:20
Martin#30-Jul-07 5:20 
GeneralRe: Dispose Method Pin
Luc Pattyn30-Jul-07 6:26
sitebuilderLuc Pattyn30-Jul-07 6:26 
GeneralRe: Dispose Method Pin
Luc Pattyn13-Aug-07 1:53
sitebuilderLuc Pattyn13-Aug-07 1:53 
GeneralRe: Dispose Method Pin
Martin#13-Aug-07 2:15
Martin#13-Aug-07 2:15 
GeneralRe: Dispose Method Pin
Guffa30-Jul-07 22:09
Guffa30-Jul-07 22:09 
GeneralRe: Dispose Method Pin
Luc Pattyn30-Jul-07 22:49
sitebuilderLuc Pattyn30-Jul-07 22:49 
GeneralRe: Dispose Method Pin
Guffa2-Aug-07 7:47
Guffa2-Aug-07 7:47 
Luc Pattyn wrote:
You assign magic powers to the GC; it does not understand your program


Yes, it does. All it has to do is to check where the variables are used. That's not very complicated.

Luc Pattyn wrote:
it can't
predict whether an existing reference is still needed or not.


Yes, it can, and it does.

Luc Pattyn wrote:
The local ref-type variable is located on the stack (or JIT-optimized into a
register). If it is not null, the object it refers to is alive, even if the
program does not intend to use it any more.


That's not accurate. The object is only alive as long as the reference to it is used. When the garbage collector knows that the referens is not going to be used any more, the reference is no longer considered, and the object is no longer alive.

This can easily be demonstrated with this program:

using System;
using System.Collections.Generic;

namespace TestConsole {

	class Program {

		static List<WeakReference> refs; 

		static void Main(string[] args) {
			refs = new List<WeakReference>();
			byte[] ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, ref10;

			ref1 = Create();
			ref2 = Create();
			ref3 = Create();
			ref4 = Create();
			ref5 = Create();
			ref6 = Create();
			ref7 = Create();
			ref8 = Create();
			ref9 = Create();
			ref10 = Create();

			int index = 1;
			foreach (WeakReference r in refs) {
				Console.WriteLine("Item {0} : {1}", index, r.IsAlive);
			}
		}

		static byte[] Create() {
			byte[] r = new byte[100000000];
			refs.Add(new WeakReference(r));
			return r;
		}

	}

}

When you run it, it will show that only one (at least on my system) of the arrays is still alive. The others have been garbage collected, despite there being specific references to them.

If you try it, be sure to run it in release mode. In debug mode the references are considered active for their entire scope, so that the debugger can show their values.

Luc Pattyn wrote:
Now my problem was: why would the compiler actually generate code to assign
null to it, since there is no further use for it; a regular non-CLR compiler
(say a C compiler) would optimize it away immediately. But it seems the C# compiler
designer has not optimized assignments that far that "obj=null;" would be
removed. (Unfortunately he also did not remove useless val-type assignments,
try ending a method on int a=12; and watch its MSIL).


The compiler doesn't do much optimising. Almost all optimising is done by the JIT compiler, so you should look at the actual code generated, not the IL code.

---
single minded; short sighted; long gone;

GeneralRe: Dispose Method Pin
Luc Pattyn2-Aug-07 8:23
sitebuilderLuc Pattyn2-Aug-07 8:23 
GeneralRe: Dispose Method Pin
Guffa4-Aug-07 1:08
Guffa4-Aug-07 1:08 
GeneralRe: Dispose Method [modified] Pin
Luc Pattyn4-Aug-07 14:48
sitebuilderLuc Pattyn4-Aug-07 14:48 
GeneralRe: Dispose Method Pin
Luc Pattyn2-Aug-07 21:56
sitebuilderLuc Pattyn2-Aug-07 21:56 
GeneralRe: Dispose Method Pin
Guffa4-Aug-07 1:19
Guffa4-Aug-07 1:19 
AnswerRe: Dispose Method Pin
Karthi_jpk30-Jul-07 3:19
Karthi_jpk30-Jul-07 3:19 
AnswerRe: Dispose Method Pin
Scott Dorman31-Jul-07 14:29
professionalScott Dorman31-Jul-07 14:29 
GeneralRe: Dispose Method Pin
N a v a n e e t h31-Jul-07 18:21
N a v a n e e t h31-Jul-07 18:21 
GeneralRe: Dispose Method Pin
Scott Dorman31-Jul-07 19:43
professionalScott Dorman31-Jul-07 19:43 
QuestionConnecting to SQL Server through C#.net Pin
Saba0230-Jul-07 2:32
Saba0230-Jul-07 2:32 
AnswerRe: Connecting to SQL Server through C#.net Pin
originSH30-Jul-07 3:03
originSH30-Jul-07 3:03 
AnswerRe: Connecting to SQL Server through C#.net Pin
Karthi_jpk30-Jul-07 3:04
Karthi_jpk30-Jul-07 3:04 
GeneralRe: Connecting to SQL Server through C#.net [modified] Pin
originSH30-Jul-07 3:19
originSH30-Jul-07 3:19 
QuestionAnother instance of my program is already running? Pin
pmartike30-Jul-07 2:17
pmartike30-Jul-07 2:17 
AnswerRe: Another instance of my program is already running? [modified] Pin
Martin#30-Jul-07 2:22
Martin#30-Jul-07 2:22 
QuestionSMO Impersonation? Pin
NanaAM30-Jul-07 1:47
NanaAM30-Jul-07 1:47 
QuestionSir Who redirect Fonts from C:\WINDOWS\Fonts to my Sample Application ? Pin
CodeVarma30-Jul-07 1:37
CodeVarma30-Jul-07 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.