- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在 C# 中有一个计时器,它在它的方法中执行一些代码。在代码中我使用了几个临时对象。
如果我在方法中有类似 Foo o = new Foo();
的东西,这是否意味着每次计时器滴答作响时,我都会创建一个新对象和一个新对象引用该对象?
如果我有 string foo = null
然后我只是在 foo 中放了一些临时的东西,它和上面一样吗?
垃圾收集器是否曾经删除过对象并且引用或对象会不断创建并保留在内存中?
如果我只是声明 Foo o;
而没有将它指向任何实例,那不是在方法结束时释放了吗?
如果我想确保删除所有内容,最好的方法是:
Foo o;
放在计时器的方法之外,并在内部进行赋值 o = new Foo()
,这样指向对象的指针就会在方法结束,垃圾收集器将删除该对象。最佳答案
1.If I have something like Foo o = new Foo(); inside the method, does that mean that each time the timer ticks, I'm creating a new object and a new reference to that object?
是的。
2.If I have string foo = null and then I just put something temporal in foo, is it the same as above?
如果你问行为是否相同,那么是的。
3.Does the garbage collector ever delete the object and the reference or objects are continually created and stay in memory?
这些对象使用的内存肯定是在引用被认为未使用后收集的。
4.If I just declare Foo o; and not point it to any instance, isn't that disposed when the method ends?
不,因为没有创建对象,所以没有要收集的对象(dispose 不是正确的词)。
5.If I want to ensure that everything is deleted, what is the best way of doing it
如果对象的类实现了IDisposable
,那么你肯定想尽快贪婪地调用Dispose
。 using
关键字使这更容易,因为它以异常安全的方式自动调用 Dispose
。
除了停止使用该对象之外,您实际上不需要做任何其他事情。如果引用是一个局部变量,那么当它超出范围时,它将有资格被收集。1 如果它是一个类级别的变量,那么您可能需要分配 null
使其在包含类符合条件之前使其符合条件。
1这在技术上是不正确的(或者至少有点误导)。一个对象在超出范围之前就可以被收集。 CLR 被优化为在检测到不再使用引用时收集内存。在极端情况下,即使其中一个方法仍在执行,CLR 也可以收集对象!
更新:
这里是一个例子,展示了 GC 将收集对象,即使它们可能仍在范围内。您必须编译发布版本并在调试器之外运行它。
static void Main(string[] args)
{
Console.WriteLine("Before allocation");
var bo = new BigObject();
Console.WriteLine("After allocation");
bo.SomeMethod();
Console.ReadLine();
// The object is technically in-scope here which means it must still be rooted.
}
private class BigObject
{
private byte[] LotsOfMemory = new byte[Int32.MaxValue / 4];
public BigObject()
{
Console.WriteLine("BigObject()");
}
~BigObject()
{
Console.WriteLine("~BigObject()");
}
public void SomeMethod()
{
Console.WriteLine("Begin SomeMethod");
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("End SomeMethod");
}
}
在我的机器上运行终结器,而 SomeMethod
仍在执行!
关于c# - 在 C# 中释放内存的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6066200/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!