Weak Reference

本文简述dotNet中Weak Reference(弱引用)的功能,并举例在具体应用场景中的效果。

 

首先,让我们回忆一下dotNet CLR的对象生命周期管理。为了解决对象引用计数器以及编码不慎可能导致的内存泄漏,dotNet CLR引入了Garbage Collection(GC)机制。GC会不时的扫描程序,将程序中不再使用的对象进行回收。

那么,怎样的对象才是不再被使用的呢?通常的,我们可以简单认为,在未关联至任何变量(包括对象属性、集合内成员等)的情况下的对象,也就是说,无法被安全的访问到的对象,会被认为是不再被使用的对象。那么,有没有例外呢?

这个例外,就是dotNet中的Weak Reference。它的直接功能,就是封装对一个对象的引用,但不会被GC认做有效的关联。换言之,GC在判断对象是否不再被使用时,不会考虑Weak Reference的引用。

Weak Reference类位于System命名空间下。我们可以通过其构造器直接构造一个对象。System.WeakReference有两个构造器,通常我们使用的第一个,要求传送一个object作为其引用的实际对象(Target属性);第二个构造器需要传送一个bool类型变量,用以定义是否进行长程跟踪,本功能不在本文中进行描述。

当我们构造好一个System.WeakReference后,我们可以通过访问其Target属性,设置或读取其引用的实际变量。由于GC不会考虑Weak Reference中对象的引用,如果Target属性的对象仅被引用于Weak Reference对象时,这个对象则随时可能被GC回收。

我们可以通过判断Weak Reference对象的IsAlive属性,来确定Target属性指向的对象是否已经被回收。当IsAlive为false时,Target属性也将被自动赋值为null。特别的,由于Target属性也可以被显式的赋值为null,我们应该使用IsAlive来进行判断,而不要使用Target属性是否为null来推断。

 

那么,Weak Reference有什么实际用途呢?

有时候,我们需要访问一个对象,却又不想管理其生命周期。例如,我们可能在要某段代码中,将某个窗体对象赋值至一个对象属性中,但我们又不希望由于这个对象未被回收,影响到原窗体的生命周期。如果我们使用Weak Reference对象,代替原属性值,即可避免这种情况的出现。

另外,有些数据,我们并不会经常的访问,但所有的访问又相对密集。这时候我们也可以使用Weak Reference来对数据进行封装。每次访问时,通过判断IsAlive来确定,是否需要构造其Target。由于GC通常不会在系统繁忙时激活,所以这种类似Cache的应用,通常只会在资源不足或系统空闲时才会被回收,而且这种回收是由dotNet CLR负责实现,无需用户编码干预。

拒酒话,你会说吗?

原帖:http://blog.renren.com/blog/358840632/741238999

拒酒词、与领导喝酒的诀窍、酒量不行的技巧 
酒桌这个交际场所,是挺考验人的。你不能喝酒,最好学会拒酒;你不能酒量让新友们痛快,那就凭三寸不烂之舌让大伙儿开心。这样,你即时伤自己的身体,又不让劝酒者扫兴。下面介绍几条“拒酒词”,你看好使不?

一、只要感情好,能喝多少,喝多少 

你可以展开说:“九千九百九十九朵玫瑰也难成全一个爱情。只有感情不够。才用玫瑰来凑。因此,只要感情好,能喝多少,喝多少。我不希望我们的感情掺合那么多’水分’。我虽然喝了一点儿,但这一点儿是一滴浓浓的情。点点滴滴都是情嘛!”

二、只要感情到了位,不喝也会陶醉 

你试试这样说:“跟你不喜欢的人在一起喝酒,是一种苦痛;跟你喜欢的人在一起喝酒,是一种感动。我们走到一块,说明我们感情到了位。只要感情到了位,不喝也陶醉。

三、只要感情有,喝什么都是酒

你如果确实不能沾酒,就不妨说服对方,以饮料或茶水代酒。你问他:“我俩有没有感情?”他会答:“有!”你顺势说:“只要感情有,喝什么都是酒。感情是什么?感情就是理解,理解万岁!”你然后以茶代酒,表示一下。

四、感情浅,哪怕喝大碗;感情深,哪怕舔一舔 

酒桌上,千言万语,无非归结一个字“喝”。如:“你不喝这杯酒,一定嫌我长得丑。”如:“感情深,一口吞;感情浅,舔一舔。”劝酒者把喝酒的多少与人的美丑和感情的深浅扯到一块。你可以驳倒它们的联系:“如果感情的深浅与喝酒的多少成正比,我们这么深的感情,一杯酒不足以体现。我们应该跳进酒缸里,因为我们多年交情,清深似海。其实,感情浅,哪怕喝大碗;感情深,哪怕舔一舔。”

五、为了不伤感情,我喝;为了不伤身体,我喝一点

他劝你:“喝!感情铁,喝出血!宁伤身体,不伤感情;宁把肠胃喝个洞,也不让感情裂个缝!”这是不理性的表现,你可以这样回答:“我们要理性消费,理性喝酒。’留一半清醒,留一半西醉,至少在梦里有你伴随’,我是身体和感情都不愿伤害的人。没有身体,就不有能体现感情;没有感情,就是行尸走肉!为了不伤感情,我喝;为了不伤身体,我喝一点儿。”

六、在这开心一刻,让我们来做选择题吧! 

我们思路打开一些,拒酒的办法就来了。他要借酒表达对你的情和意,你便说;“开心一刻是可以做选择题的。表达情和意,可以:A.拥抱,B.拉手,C.喝酒,任先选一项。我敬你,就让你选;你敬我,应该该让我选。现在,我选择A.拥抱,好吗?”

七、君子动口,不动手 

他要你干杯,你可以巧没“二难”,请君入瓮。你问他:“你是愿意当君子还是愿意当小人?请你先回答这个问题。”他如果说“愿意当君子”,你便说“君子之交,淡如水”,以茶水代酒,或者说“君子动口,不动手,你动口喝”,请他喝;他如果说“愿意当小人”,你便说“我不跟小人喝酒”,然后笑着坐下,他也无可奈何。

总之,拒酒词、拒酒的办法还有很多,要随机应变,“兵来将挡“。酒文化中既有劝酒词,也有拒酒词,你没有酒量,凭着你的机智和口才也可以在交际场上应对,游刃有余。

钢琴曲Kanon(卡农)的由来

Pachelbel(帕赫贝尔卡农的作者1653-1706),德国人。在他10几岁的时候,战乱使他沦为孤儿。流浪到英国的他被英国一个小村庄的天天在教堂弹琴的琴师收养,之后他天天听那个他弹琴,耳濡目染也学会了钢琴。

在他们旁边的镇上上有一个女孩子叫Barbara Gabler,家里有钱有势,Barbara Gabler也是镇上最漂亮的女孩,自从到教堂听了Pachelbel弹的曲子,就爱上了他。很多有钱人上门向Barbara Gabler提亲都被拒绝了,因为Barbara Gabler心里只喜欢Pachelbel。但女孩子嘛,都比较害羞,从小被宠到大的Barbara Gabler一直不敢向Pachelbel表白,后来Barbara Gabler就找了个理由,说要去Pachelbel那里学钢琴,她对Pachelbel说自己热爱音乐,希望可以拜师学艺。

Pachelbel很高兴的收下了这个徒弟。但Barbara Gabler她的目的并不是弹琴,所以几乎不把精力花费在钢琴上,遭到了Pachelbel一再的责骂。Barbara Gabler心里委屈,但还是一直跟着Pachelbel,希望Pachelbel能明白自己的心意。

终于有一天Pachelbel对Barbara Gabler说:“你走吧,你真的不适合弹钢琴。而且你也不喜欢钢琴。”Barbara Gabler听后,对自己说:“不要说我不行!Pachelbel。我回去一定要好好弹琴,半年后我要拿到本地的钢琴第一名的!”(在镇上每年都会举行一次女孩钢琴大赛。)

半年里,Barbara Gabler天天练习,饿了就叫家里的用人送些吃的,困了就趴着睡一会。 半年一转眼就过去了,Barbara Gabler参加了比赛,果真的拿了奖。Barbara Gabler想拿这个奖杯去找Pachelbel并向他表达自己的爱意,但当她去找Pachelbel的时候,Pachelbel已经不在了。当时正值战乱,Pachelbel被征去打仗,Barbara Gabler知道后说:“好,我等他回来。”

就这样Barbara Gabler等了Pachelbel整整3年多,在这期间村长的儿子看上了Barbara Gabler ,村长的儿子很清楚Barbara Gabler已经心有所属,就叫人从前线运回来一具碎尸体,说那就是Pachelbel。Pachelbel没有父母和亲戚,没有人可以对证,Barbara Gabler相信Pachelbel真的已经死了,趴在"Pachelbel"的尸体上哭了3天3夜,那时,村长的儿子买了很多的礼物去找Barbara Gabler提亲,Barbara Gabler没有理睬。

在3天后的晚上,在当时Pachelbel教Barbara Gabler钢琴的教堂里,Barbara Gabler割腕自杀了。

而Pachelbel在Barbara Gabler离开的半年里,他发现没有Barbara Gabler在身边,自己少了很多很多的快乐,其实很多事情就是这样失去了才知道珍惜。Pachelbel在Barbara Gabler离开后才发现原来自己已经不知不觉的爱Barbara Gabler,只是因为她学琴不努力所以就埋没了对她的喜欢。当时他准备写一首歌,做为向Barbara Gabler求婚的礼物,当他完成了卡农的1/3的时候。他被招去打仗了,在战乱中,自己的生命多次都是九死一生,每当心中不舒服的时候都会想到Barbara Gabler,想到教她弹琴的日子……那段时光真是值得怀念啊。之后他完成了卡农剩下的2/3。

在Barbara Gabler自杀后了第2个月,Pachelbel回到了村里。他从村民的口中知道了Barbara Gabler的故事和她为自己做的所有事后,他咆哮着,放声大哭。他找到了Barbara Gabler的家人,问她现在葬在哪。她家里人都不肯告诉Pachelbel,随后的一次礼拜,Pachelbel招集他们村和Barbara Gabler村上所有的人,他坐在钢琴前强忍着泪水,弹出了卡农“Canon(D Major)”弹后,在场的所有人没有一个没有落泪的。

Canon,一个始终能让爱与缠绵生死相随的名词。

人生的悲欢离合,如同移换间反射的光影,音乐的诗意控制着生命的悲喜,浸染心灵。无论是内敛的沧桑,还是未知寒意的苍茫,深爱的信念能超越自我潜能,穿透古朴夜色回归忘却的姻缘,前世的约定亦会不远万里,漫过隐约的秋水,触礁曾经的爱人。

50 reasons not to date a graphic designer

From http://abourbonforsilvia.wordpress.com/2010/11/10/50-reasons-not-to-date-a-graphic-designer/

(in bold my most representatives)

1. They are very weird people.
2. There are billions of them in the world, like colors on the screen of your computer.
3. They will analyse conversations in layers.
4. You will spend the day assembling furniture from IKEA.
5. They drink and eat all kinds of weird shit just because they like the packaging.
6. They hate each other.
7. You’ll come out the last out of the movies because you have to see the full list of credits.
8. They cant change a light bulb or without making a sketch.
9. They fuck up all the tables with their cutters.
10. They rather study the paisley pattern on your outfit than listen to what you have to say.
11. They will fill your house with magazines and whatever is out there that has drawings.
12. You never know if it is really an original or a copy.
13. They make collages with your photos.
14. They do not know how to add and subtract, they just understand letters.
15. They idolize people who nobody knows and speak of them as if they were his colleagues.
16. They take pictures almost daily and all are cut in weird shapes.
17. They ask your opinion about everything but  they do whatever they want.
18. Everything is left justified, right or center unless they arrive late.
19. They hate Comic Sans with the same passion they love Helvetica.
20. They use iPhone for everything, because everyone has one.
21. You can not decorate the house without consulting them.
22. They steal street signs.
23. Always carry their hands painted with something.
24. They buy dolls unfinished for them to paint.
25. Everything becomes something other than what it really is: cards as tickets, cards as …
26. When arguing, you will be nicknamed like the OSX spinning wheel (not affectionately)
27. Do not know how to dress without consulting the Pantone book.
28. They hate Excel.
29. They read comics.
30. They want to save the world only with a poster.
31. You will spend the day brainstorming.
32. On vacation they will take you to countries that you do not know exist and have no beach.
33. Museums are their second home.
34. They know more positions than the Kamasutra.
35. They can’t go to a restaurant without secretly critiquing the menu design.
36. They listen to music you have never heard of.
37. They can´t cook a normal dish, they always have to experiment with new ingredients.
38. They read rare books: stories of children, Semiotics …
39. When you are going to tell you something, everyone has read it in their facebook and twitter.
40. They have own iPods before you knew they existed.
41. The orgasm they remember is when they heard that Adobe was acquiring Macromedia.
42. They have their own shops just for them and there are the most expensive in the city.
43. They want to spend all the money in the Apple Store.
44. You will never understand their gifts.
45. They see ordinary objects and laugh.
46. You wake up in the middle of the night hearim them screaming “When is the deadline?”
47. They see CMYK and RGB like Neo sees the Matrix.
48. They dream of the day nobody will make a single change to their designs.
49. They rather pay for a font than for a special birthday gift.
50. They are always sleepy because they work 24/7.

Sharepoint Database Permission

If you wanna deploy Sharepoint 2010 with a SqlServer 2008 R2 ON ANOTHER SERVER, make sure that the domain account which will be used as Sharepoint Service has the dbcreator and securityadmin permission in the target SqlServer.

If securityadmin is not selected, you may have trouble to setup a search service application. All you need is delete the deployed search service application, add permission to SqlServer and re-create a new search service application.

Fix the ESXi not responding problem

Situation:

A “not responding” are displayed after all esxi hypervisor shown in vsphere client which linked to the vcenter. Right click any of the esxi and select “Connect” will bring it back. But after a short while, no more than 90 secs, it will be back to not responding status.

Solution:

1 In vShpere client, open menu “Administration” — “vCenter Server Settings”, click “Runtime Settings” from the left panel, make sure that all IP and server name are correct. If not, re-enter it. Close and reopen your vSphere client — This is important.

2 Try to ping from your vCenter, not the vSphere client, to each of esxi hypervisors.

3 If you are using Windows Server 2008/R2 or any OS with firewall for vCenter, you need to add or change some firewall rules:

change:

Enable the “File and Printer Sharing (Echo Request – ICMPv4-In)”

Enable the “File and Printer Sharing (Echo Request – ICMPv6-In)”

Add a rule to allow TCP 903 income for ESXi heartbeat.

Add a rule to allow UDP 902 income for ESXi heartbeat.

4 Try to reconnect all ESXi hypervisors.

 

If the problem still exists, you have to google it again.