finalize

从C#垃圾回收(GC)机制中挖掘性能优化方案

戏子无情 提交于 2019-12-25 11:03:00
  GC,Garbage Collect,中文意思就是垃圾回收,指的是系统中的内存的分配和回收管理。其对系统性能的影响是不可小觑的。今天就来说一下关于GC优化的东西,这里并不着重说概念和理论,主要说一些实用的东西。关于概念和理论这里只做简单说明,具体的大家可以看微软官方文档。 一、什么是GC   GC如其名,就是垃圾收集,当然这里仅就内存而言。Garbage Collector(垃圾收集器,在不至于混淆的情况下也成为GC)以应用程序的root为基础,遍历应用程序在Heap上动态分配的所有对象[2],通过识别它们是否被引用来确定哪些对象是已经死亡的、哪些仍需要被使用。已经不再被应用程序的root或者别的对象所引用的对象就是已经死亡的对象,即所谓的垃圾,需要被回收。这就是GC工作的原理。为了实现这个原理,GC有多种算法。比较常见的算法有Reference Counting,Mark Sweep,Copy Collection等等。目前主流的虚拟系统.NET CLR,Java VM和Rotor都是采用的Mark Sweep算法。(此段内容来自网络) .NET的GC机制有这样两个问题:   首先,GC并不是能释放所有的资源。它不能自动释放非托管资源。   第二,GC并不是实时性的,这将会造成系统性能上的瓶颈和不确定性。   GC并不是实时性的,这会造成系统性能上的瓶颈和不确定性

C#垃圾回收

此生再无相见时 提交于 2019-12-25 11:02:37
很多系统都有其自身的垃圾回收,其回收机制大体是相同的。它们使程序员从跟踪内存使用的繁重任务中解脱出来。虽然大多数回收器都要求应用程序不时地暂停从而释放不再使用的内存。但C#中的回收器效率还是很高的。 垃圾回收器的基本假定: 1.被分配内存空间的对象最有可能被释放。在方法执行时,就需要为该方法的对象分配内存空间,搜索最近分配的对象集合有助于花费最少的代价来尽可能多地释放内存空间。 2.生命期最长的对象释放的可能性最小,经过几轮垃圾回收后,对象仍然存在,搜索它时就需要进行大量的工作,却只能释放很小的一部分空间。 3.同时被分配内存的对象通常是同时使用,将它们彼此相连有助于提高缓存性能和回收效率 。 C#中的回收器是分代的垃圾回收器(Gererational Garbage Collector) 它将分配的对象分为3个类别或代。(可用GC.GetGeneration方法返回任意作为参数的对象当前所处的代) 最近被分配内存的对象被放置于第0代,因为第0代很小,小到足以放进处理器的二级(L2)缓存,所以它能够提供对对象 的快速存取。经过一轮垃圾回收后,仍然保留在第0代中的对象被移进第1代中,再经过一轮垃圾内存回收后,仍然保留在第1代中的对象则被移进第2代中,第2代中包含了生存期较长的对象。 在C#中值类型是在堆栈中分配内存,它们有自身的生命周期,所以不用对它们进行管理,会自动分配和释放

Why does the finalize function not get called in this unit test?

痴心易碎 提交于 2019-12-24 17:52:06
问题 I'm trying to write a Java unit test that tests the effects of a call to a finalizer on an object. In order to be sure the finalizer gets called I'm using a WeakReference method I saw elsewhere on stackoverflow. My problem is that in this test the finalize method of TestFinalizer never gets called even though the WeakReference comes up null after just one iteration: public class FinalizerTest { private static class TestFinalizer { public static class Callback { public int NumFinalize = 0;

GC的前世与今生

倾然丶 夕夏残阳落幕 提交于 2019-12-24 16:39:08
GC的前世与今生 虽然本文是以.net作为目标来讲述GC,但是GC的概念并非才诞生不久。早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是GC的第一次出现。Lisp的程序员认为内存管理太重要了,所以不能由程序员自己来管理。但后来的日子里Lisp却没有成气候,采用内存手动管理的语言占据了上风,以C为代表。出于同样的理由,不同的人却又不同的看法,C程序员认为内存管理太重要了,所以不能由系统来管理,并且讥笑Lisp程序慢如乌龟的运行速度。的确,在那个对每一个Byte都要精心计算的年代GC的速度和对系统资源的大量占用使很多人的无法接受。而后,1984年由Dave Ungar开发的Small talk语言第一次采用了Generational garbage collection的技术(这个技术在下文中会谈到),但是Small talk也没有得到十分广泛的应用。 直到20世纪90年代中期GC才以主角的身份登上了历史的舞台,这不得不归功于Java的进步,今日的GC已非吴下阿蒙。Java采用VM(Virtual Machine)机制,由VM来管理程序的运行当然也包括对GC管理。90年代末期.net出现了,.net采用了和Java类似的方法由CLR(Common Language Runtime)来管理

GC的前世与今生

寵の児 提交于 2019-12-24 16:38:54
   原文地址:http://kb.cnblogs.com/page/106720/   作者: spring yang GC的前世与今生   虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久。早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是GC的第一次出现。Lisp的程序员认为内存管理太重要了,所以不能由程序员自己来管理。   但后来的日子里Lisp却没有成气候,采用内存手动管理的语言占据了上风,以C为代表。出于同样的理由,不同的人却又不同的看法,C程序员认为内存管理太重要了,所以不能由系统来管理,并且讥笑Lisp程序慢如乌龟的运行速度。的确,在那个对每一个Byte都要精心计算的年代GC的速度和对系统资源的大量占用使很多人的无法接受。而后,1984年由Dave Ungar开发的Smalltalk语言第一次采用了Generational garbage collection的技术(这个技术在下文中会谈到),但是Smalltalk也没有得到十分广泛的应用。   直到20世纪90年代中期GC才以主角的身份登上了历史的舞台,这不得不归功于Java的进步,今日的GC已非吴下阿蒙。Java采用VM(Virtual Machine)机制,由VM来管理程序的运行当然也包括对GC管理。90年代末期.NET出现了,

Do I need to implement a dispose or finalize in my objects?

天大地大妈咪最大 提交于 2019-12-23 12:10:05
问题 For too long I let the garbage collector do its magic, removing all responsibilities from my self. Sadly it never turned into an issue... So I never gave a second thought to the subject. Now when I think about it I don't really understand what the "dispose" function really does and how and when it should be implemented. The same question for finalize... And a last question... I have a class pictureManipulation : when I need to save/resize/change format ... I start a new instance of that class

final,finally,finalize

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 01:06:33
final:可以修饰属性,可以修饰方法(方法不能被重写,可以继承),可以修饰类(该类不能被继承,不能产生子类) finally:无论什么情况,都会执行 finalize:垃圾回收时,调用此方法 来源: https://www.cnblogs.com/mataoblogs/p/10771063.html

when finalize() is being executed? [duplicate]

久未见 提交于 2019-12-22 16:46:08
问题 This question already has answers here : When is the finalize() method called in Java? (17 answers) Closed 5 years ago . In an interview i was asked,suppose JVM runs gc when object of class A is not in used. class A{ //some code here protected void finalize(){ //code here } } does it guarantee the execution of finalize(). I said yes The next ques was if obj of Class A is being used, if now JVM runs GC does it execute finalize(). I said no, it'll not execute this finalize() as JVM does not

Why do I need to call a close() or shutdown() method?

假如想象 提交于 2019-12-22 07:28:27
问题 I'm new in Java with some background in C++ in my High School years. Now I'm trying to make something and I chose Java as the programming language. I've done my homework and look a lot about "destructors" for Java, finalize() method, and close() or shutdown() methods. But still I think I don't have the idea of how this should work (more info below of course) OK, the concrete question would be why do I need to call close() or shutdown() methods? In my particular case I'm working with a class

Why do I need to call a close() or shutdown() method?

拜拜、爱过 提交于 2019-12-22 07:28:06
问题 I'm new in Java with some background in C++ in my High School years. Now I'm trying to make something and I chose Java as the programming language. I've done my homework and look a lot about "destructors" for Java, finalize() method, and close() or shutdown() methods. But still I think I don't have the idea of how this should work (more info below of course) OK, the concrete question would be why do I need to call close() or shutdown() methods? In my particular case I'm working with a class