问一基础问题

king2003 2008-08-25 09:15:43
User u;
for(i=1;i<10;i++)
{
u=new User();
}

前面9个u访问不到了,垃圾回收器不回收吗?为什么我还能使用前面的9个对象
...全文
433 52 打赏 收藏 转发到动态 举报
写回复
用AI写文章
52 条回复
切换为时间正序
请发表友善的回复…
发表回复
lude8880 2008-08-27
  • 打赏
  • 举报
回复
就是狗熊掰棒子,其它几个棒子都不在了,就像int i=0; i=1;i=2一样
king2003 2008-08-27
  • 打赏
  • 举报
回复
看后面的
luozuguo 2008-08-27
  • 打赏
  • 举报
回复
u没有指向前面9个对象,但它们还存在,只是无法访问
luozuguo 2008-08-27
  • 打赏
  • 举报
回复
你每次 u=new User(); 的时候,u都会指向一个新的引用地址,前面的地址会被覆盖,所以访问不到
king2003 2008-08-27
  • 打赏
  • 举报
回复
我自横刀香甜笑来看看呀
king2003 2008-08-27
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;


namespace SqlCacheDependency
{
class Program
{
public static string connString = "server=localhost;uid=sa;pwd=123456;database=dbTest";
public static Dictionary<int, string> dt;
public static Stack<SqlDepState> ss = new Stack<SqlDepState>();

static void Main(string[] args)
{
GenerateMonitor();
Console.ReadLine();
}

public static void GenerateMonitor()
{
SqlDependency.Start(connString);

dt = new Dictionary<int, string>();
dt.Add(2, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(8, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(3, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(5, "select id,name from dbo.test where id<>4 order by id desc");
int i = 0;
SqlDepState[] sess = new SqlDepState[dt.Count];
foreach (KeyValuePair<int, string> item in dt)
{
sess[i] = new SqlDepState(item.Key, item.Value);
sess[i].RecordChanged += delegate(object sender)
{
Console.WriteLine(((SqlDepState)sender).ID.ToString());
};
i++;
}
}
}

public delegate void RecordChangeHandler(object sender);

public class SqlDepState
{
public int ID;
public string commText;
public event RecordChangeHandler RecordChanged;

public SqlDepState(int id, string commText)
{
this.ID = id;
this.commText = commText;
Init();
}

private void Init()
{
using (SqlConnection conn = new SqlConnection(Program.connString))
{
SqlCommand command = new SqlCommand(commText, conn);
command.Notification = null;
SqlDependency d = new SqlDependency(command);
conn.Open();
command.ExecuteNonQuery();
d.OnChange += delegate(object sender, SqlNotificationEventArgs e)
{
//激活事件
if (RecordChanged != null)
{
RecordChanged(this);
}
Init();
};
}

}
}
}
以前的写的太乱了,重新整理了一下
king2003 2008-08-27
  • 打赏
  • 举报
回复
没人用了就会回收掉
number321 2008-08-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 king2003 的帖子:]
User u;
for(i=1;i <10;i++)
{
u=new User();
}

前面9个u访问不到了,垃圾回收器不回收吗?为什么我还能使用前面的9个对象
[/Quote]

我也有个问题,这样的语句,编译器不会把前面的几个循环优化掉吗,还是老老实实的循环?
什么情况下会优化掉呢
king2003 2008-08-27
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using zhaotest1.BusinessLayer;


namespace SqlCacheDependency
{
class Program
{
public static string connString = "server=localhost;uid=sa;pwd=123456;database=dbTest";
public static Dictionary<int, string> dt;
public static Stack<SqlDepState> ss = new Stack<SqlDepState>();

static void Main(string[] args)
{
GenerateMonitor();
Console.ReadLine();
}

public static void GenerateMonitor()
{
SqlDependency.Start(connString);
SqlCommandTextFactory sqlCommandTextFactory = new SqlCommandTextFactory();
List<SqlCommandText> ss = sqlCommandTextFactory.GetAll();
int i = 0;
SqlDepState[] sess = new SqlDepState[ss.Count];
ss.ForEach(
#region 委托

delegate(SqlCommandText o)
{
sess[i] = new SqlDepState(o.ID, o.CommText);
sess[i].RecordChanged += delegate(object sender)
{

// Console.WriteLine("调用 " + ((SqlDepState)sender).ID.ToString() + "的接口");
CacheFactory cf = new CacheFactory();
List<Cache> cs = cf.GetAllBy(Cache.CacheFields.Sql_ID, ((SqlDepState)sender).ID);
cs.ForEach(

delegate(Cache oo) { Console.WriteLine(oo.Sql_ID+":" + oo.WebURL); }

);

};

i++;
}
#endregion
);
}
}

public delegate void RecordChangeHandler(object sender);

public class SqlDepState
{
public int ID;
public string commText;
public event RecordChangeHandler RecordChanged;

public SqlDepState(int id, string commText)
{
this.ID = id;
this.commText = commText;
Init();
}

private void Init()
{
using (SqlConnection conn = new SqlConnection(Program.connString))
{
SqlCommand command = new SqlCommand(commText, conn);
command.Notification = null;
SqlDependency d = new SqlDependency(command);
conn.Open();
command.ExecuteNonQuery();
d.OnChange += delegate(object sender, SqlNotificationEventArgs e)
{
//激活事件
if (RecordChanged != null)
{
RecordChanged(this);
}
Init();
};
}

}
}
}
king2003 2008-08-27
  • 打赏
  • 举报
回复
这算种技巧呀!再说我现在的没有丢了
  • 打赏
  • 举报
回复
给你改进了一下。就可以了。
foreach( KeyValuePair<int, string> item in dt )
{
if( i != -1 )
{
if( i == item.Key )
{
init( item.Key, item.Value );
}
}
else
{
init( item.Key, item.Value );
break;
}
}
循环逻辑出了点小问题。
  • 打赏
  • 举报
回复
问题是出在SqlDependency上。你定义了四个对象,所以他就会执行四次。跟你自己定义的类没有什么关系。这个有一个监控就行了。为什么要定义四个呢?你的那个对象还在被你的SqlDependency上引用着。所以就不会被回收,所以这个对象就得被保留着。
  • 打赏
  • 举报
回复
你这个程序执行后是什么结果?
king2003 2008-08-26
  • 打赏
  • 举报
回复
业务需要呀!!!
king2003 2008-08-26
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;


namespace SqlCacheDependency
{
class Program
{
static string connString = "server=localhost;uid=sa;pwd=123456;database=dbTest";
static void Main(string[] args)
{
intSql(-1);
Console.ReadLine();
}

public static void intSql(int i)
{


SqlDependency.Start(connString);
Dictionary<int, string> dt = new Dictionary<int, string>();
dt.Add(1, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(2, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(3, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(4, "select id,name from dbo.test where id<>4 order by id desc");
foreach (KeyValuePair<int, string> item in dt)
{
if (i != -1)
{
if (i == item.Key)
{
init(item.Key, item.Value);
}
}
else
{
init(item.Key, item.Value);
}
}
}

private static void init(int key,string value)
{
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand command = new SqlCommand("select id,name from dbo.test where id<>4 order by id desc", conn);
command.Notification = null;
SqlDependency d = new SqlDependency(command);
conn.Open();
SqlDataReader ProductsDr = command.ExecuteReader();
new SqlDepState(key, value, d);
}
}


}
class SqlDepState
{
public SqlDependency dep;
public int ID;
public string commText;

public SqlDepState(int id,string commText,SqlDependency dep)
{
this.ID = id;
this.commText = commText;
this.dep = dep;
this.dep.OnChange += delegate(object sender, SqlNotificationEventArgs e)
{
Console.WriteLine("aa");
Program.intSql(this.ID);
};
}



}
}
匿名方法版:
好多人不明白我这个是干啥 用的.我简单说一下,就是如果数据表里面的我所监视的数据变化了,我的程序就会得到通知.
我说的系统调用的对象是这部分
new SqlDepState(key, value, d);
这个SqlDepState,这个是由SQLSERVER调用的,注意我声明的时候,没有指定名字.
king2003 2008-08-26
  • 打赏
  • 举报
回复
我最后发的是新代码.
new SqlDepState(key, value, d);
这个东西
GhostAdai 2008-08-26
  • 打赏
  • 举报
回复
SqlDepState z;
...
foreach (string x in sql)
{
...
z = new SqlDepState();
z.dep = d;
z.aa();
...
}
我看到的是认为这一段对应楼主一楼提出的问题,不知道楼主所说的“能”指的是哪一段。后面代码我没找到对应的代码段,不知道楼主所说的“能”指的是哪一部分。
wanghui0380 2008-08-26
  • 打赏
  • 举报
回复
你清楚你的item是啥不

他是Dictionary集合里的子项
你又没这个集合,他当然永远存在
brallow 2008-08-26
  • 打赏
  • 举报
回复
没能完全理解你的这段代码与1楼的代码的具体对应的是哪部分。
不过有一点可以肯定:如果你能访问某一个引用的对象,那么这个对象就一定不会被垃圾收集器删除!
.NET正是通过计算对象引用的次数来判断一个对象是否可以被收集,如果你还能访问这个对象,那肯定是不能被删除的。
king2003 2008-08-26
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;


namespace SqlCacheDependency
{
class Program
{
static string connString = "server=localhost;uid=sa;pwd=123456;database=dbTest";
static void Main(string[] args)
{
intSql(-1);
Console.ReadLine();
}

public static void intSql(int i)
{


SqlDependency.Start(connString);
Dictionary<int, string> dt = new Dictionary<int, string>();
dt.Add(1, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(2, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(3, "select id,name from dbo.test where id<>4 order by id desc");
dt.Add(4, "select id,name from dbo.test where id<>4 order by id desc");
foreach (KeyValuePair<int, string> item in dt)
{
if (i != -1)
{
if (i == item.Key)
{
init(item.Key, item.Value);
}
}
else
{
init(item.Key, item.Value);
}
}
}

private static void init(int key,string value)
{
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand command = new SqlCommand("select id,name from dbo.test where id<>4 order by id desc", conn);
command.Notification = null;
SqlDependency d = new SqlDependency(command);
conn.Open();
SqlDataReader ProductsDr = command.ExecuteReader();
new SqlDepState(key, value, d);
}
}


}
class SqlDepState
{
public SqlDependency dep;
public int ID;
public string commText;

public SqlDepState(int id,string commText,SqlDependency dep)
{
this.ID = id;
this.commText = commText;
this.dep = dep;
this.dep.OnChange+=new OnChangeEventHandler(dep_OnChange);
}



void dep_OnChange(object sender, SqlNotificationEventArgs e)
{
Console.WriteLine("aa");
Program.intSql(this.ID);
}
}
}
加载更多回复(32)

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧