“新”怎么样?在循环中工作? [英] How does "new" work in a loop?

查看:54
本文介绍了“新”怎么样?在循环中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习C#。我正在编写一个程序(在
WinXP上使用Visual C#2005)将几个文件组合成一个(HKSplit是一个流行的

免费软件程序,它可以做到这一点,但它要求所有输入和输出

都在一个目录中,我希望能够将来自不同目录的文件

组合到我选择的另一个目录中。


我的程序似乎工作正常,但我想知道这个循环:

for(int i = 0; i< numFiles; i ++)

{

//读取下一个输入文件


FileStream fs = new FileStream(fileNames [i],

FileMode.Open,FileAccess.Read,FileShare.Read);

Byte [] inputBuffer = new Byte [fs.Length];


fs。读取(inputBuffer,0,(int)fs.Length);

fs.Close();


//附加到先前作为fsOut打开的输出流


fsOut.Write(inputBuffer,0,(int)inputBuffer.Length);

progBar.Value ++;

} / / 为我nt i


如你所见,对象fs和inputBuffer都被创建为

" new"每次通过循环,这可能是很多次。我没有b $ b认为这会起作用;我只是试着看看我会得到什么样的错误

消息,当它运行时我很惊讶。每次试运行

都能产生完美的效果。


那么这里发生了什么?内存是否正在被重用,或者我在堆上的对象上堆积了只会在我的程序结束时消失的内容,或者

我是否创建了大量的内存泄漏?


我可以看到在fs.Close()之后fs可能会消失,但我不会理解为什么我可以重新创建字节数组一遍又一遍,

没有处理它。我已经验证了调试器

每次输入文件大小改变时,数组都有不同的大小,

因此每次循环都会重新分配它,而不是

而不仅仅是被重用。我试图找到关于新

如何在循环中起作用的解释,但到目前为止我还没能做到。任何帮助,

包括指向VS文档的指针或一本关于C#的热门书籍,都会受到赞赏。

I''m just learning C#. I''m writing a program (using Visual C# 2005 on
WinXP) to combine several files into one (HKSplit is a popular
freeware program that does this, but it requires all input and output
to be within one directory, and I want to be able to combine files
from different directories into another directory of my choice).

My program seems to work fine, but I''m wondering about this loop:
for (int i = 0; i < numFiles; i++)
{
// read next input file

FileStream fs = new FileStream(fileNames[i],
FileMode.Open, FileAccess.Read, FileShare.Read);
Byte[] inputBuffer = new Byte[fs.Length];

fs.Read(inputBuffer, 0, (int)fs.Length);
fs.Close();

//append to output stream previously opened as fsOut

fsOut.Write(inputBuffer, 0, (int) inputBuffer.Length);
progBar.Value++;
} // for int i

As you can see, the objects fs and inputBuffer are both created as
"new" each time through the loop, which could be many times. I didn''t
think this would work; I just tried it to see what kind of error
message I would get, and I was surprised when it ran. Every test run
has produced perfect results.

So what is happening here? Is the memory being reused, or am I piling
up objects on the heap that will only go away when my program ends, or
am I creating a huge memory leak?

I can see that fs might go away after fs.Close(), but I don''t
understand why I''m allowed to recreate the byte array over and over,
without ever disposing of it. I have verifed with the debugger that
the array has a different size each time the input file size changes,
so it really is being reallocated each time through the loop, rather
than just being reused. I''ve tried to find explanations of how "new"
works in a loop, but I haven''t been able to so far. Any help,
including pointers to the VS docs or a popular book on C#, would be
appreciated.

推荐答案

好吧,新始终创建对象的新实例。在上一个周期中分配给变量的对象是
将被标记,因此

垃圾收集器可以释放内存。现在,由于GC没有立即清除内存中的
内存,有一段时间旧对象保留在内存中,所以你也可以使用更改GC之前的内存很多

被调用,但不是一个很好的机会。除非你打算加入每个部分都是大型演出的文件,否则我不会担心这个问题。 :)


" Tony Sinclair" < no@spam.com写信息

新闻:jv ****************************** ** @ 4ax.com ...
Well, "new" always create a new instance of the object. The object was
assigned to the variable during the previous cycle will be flagged so the
garbage collector can release memory. Now, since the GC does not clear
memory immediately, there is a period of time when the old object stays in
memory so there is the change that you can use too much memory before the GC
is invoked, but not a great chance. It is not something that I would worry
about unless you plan to join files where each part is a gig in size. :)


"Tony Sinclair" <no@spam.comwrote in message
news:jv********************************@4ax.com...

我只是在学习C#。我正在编写一个程序(在
WinXP上使用Visual C#2005)将几个文件组合成一个(HKSplit是一个流行的

免费软件程序,它可以做到这一点,但它要求所有输入和输出

都在一个目录中,我希望能够将来自不同目录的文件

组合到我选择的另一个目录中。


我的程序似乎工作正常,但我想知道这个循环:


for(int i = 0; i< numFiles; i ++)

{

//读取下一个输入文件


FileStream fs = new FileStream(fileNames [i],

FileMode.Open,FileAccess.Read,FileShare.Read);

Byte [] inputBuffer = new Byte [fs.Length];


fs.Read(inputBuffer,0,(int)fs.Length);

fs.Close();


//附加到输出流之前作为fsOut打开


fsOut.Write(inputBuffer,0,(int)inputBuffer.Length);

progBar.Value ++;

} // for int i


如您所见,对象fs和inputBuffer都被创建为

" new"每次通过循环,这可能是很多次。我没有b $ b认为这会起作用;我只是试着看看我会得到什么样的错误

消息,当它运行时我很惊讶。每次试运行

都能产生完美的效果。


那么这里发生了什么?内存是否正在被重用,或者我在堆上的对象上堆积了只会在我的程序结束时消失的内容,或者

我是否创建了大量的内存泄漏?


我可以看到在fs.Close()之后fs可能会消失,但我不会理解为什么我可以重新创建字节数组一遍又一遍,

没有处理它。我已经验证了调试器

每次输入文件大小改变时,数组都有不同的大小,

因此每次循环都会重新分配它,而不是

而不仅仅是被重用。我试图找到关于新

如何在循环中起作用的解释,但到目前为止我还没能做到。任何帮助,

包括指向VS文档的指针或一本关于C#的热门书籍,都将获得赞赏。
I''m just learning C#. I''m writing a program (using Visual C# 2005 on
WinXP) to combine several files into one (HKSplit is a popular
freeware program that does this, but it requires all input and output
to be within one directory, and I want to be able to combine files
from different directories into another directory of my choice).

My program seems to work fine, but I''m wondering about this loop:
for (int i = 0; i < numFiles; i++)
{
// read next input file

FileStream fs = new FileStream(fileNames[i],
FileMode.Open, FileAccess.Read, FileShare.Read);
Byte[] inputBuffer = new Byte[fs.Length];

fs.Read(inputBuffer, 0, (int)fs.Length);
fs.Close();

//append to output stream previously opened as fsOut

fsOut.Write(inputBuffer, 0, (int) inputBuffer.Length);
progBar.Value++;
} // for int i

As you can see, the objects fs and inputBuffer are both created as
"new" each time through the loop, which could be many times. I didn''t
think this would work; I just tried it to see what kind of error
message I would get, and I was surprised when it ran. Every test run
has produced perfect results.

So what is happening here? Is the memory being reused, or am I piling
up objects on the heap that will only go away when my program ends, or
am I creating a huge memory leak?

I can see that fs might go away after fs.Close(), but I don''t
understand why I''m allowed to recreate the byte array over and over,
without ever disposing of it. I have verifed with the debugger that
the array has a different size each time the input file size changes,
so it really is being reallocated each time through the loop, rather
than just being reused. I''ve tried to find explanations of how "new"
works in a loop, but I haven''t been able to so far. Any help,
including pointers to the VS docs or a popular book on C#, would be
appreciated.



在每个循环期间分配内存,并在

循环结束时释放概念。实际上,内存被标记为由GC(垃圾

集合)释放,作为未来的某个时间点。我发现如果你要
这个循环只做了几次而且程序空闲了,内存将被释放,但如果这个循环涉及很多文件并且很少有空闲时间

返回系统你将耗尽内存。


这就是说我至少会把fs = null和循环结束时的inputbuffer =

null。

对于fs更好的解决方案是使用会导致GC的语句

并返回内存。


使用(FileStream fs = new FileStream(fileNames [i],FileMode.Open,

FileAccess.Read,FileShare 。阅读))

{

Byte [] inputBuffer = new Byte [fs.Length];


/// do用fs的东西


inputBuffer = null;

}


问候,

John


" Tony Sinclair" < no@spam.com写信息

新闻:jv ****************************** ** @ 4ax.com ...
The memory is allocated during each loop and in concept released when the
loop ends. In reality the memory is marked to be released by GC (garbage
collection) as some future point in time. I have found that if you were to
do this loop only a few times and the program was idle the memory would be
freed but if this loop involves a lot of files and very little idle time is
returned to the system you will run out of memory.

That being said I would at the very least place fs=null and inputbuffer =
null at the end of the loop.

A better solution for fs would be the using statment which would force GC
and return the memory.

using(FileStream fs = new FileStream(fileNames[i],FileMode.Open,
FileAccess.Read, FileShare.Read))
{
Byte[] inputBuffer = new Byte[fs.Length];

/// do something with fs

inputBuffer = null;
}

Regards,
John

"Tony Sinclair" <no@spam.comwrote in message
news:jv********************************@4ax.com...

我只是在学习C#。我正在编写一个程序(在
WinXP上使用Visual C#2005)将几个文件组合成一个(HKSplit是一个流行的

免费软件程序,它可以做到这一点,但它要求所有输入和输出

都在一个目录中,我希望能够将来自不同目录的文件

组合到我选择的另一个目录中。


我的程序似乎工作正常,但我想知道这个循环:


for(int i = 0; i< numFiles; i ++)

{

//读取下一个输入文件


FileStream fs = new FileStream(fileNames [i],

FileMode.Open,FileAccess.Read,FileShare.Read);

Byte [] inputBuffer = new Byte [fs.Length];


fs.Read(inputBuffer,0,(int)fs.Length);

fs.Close();


//附加到输出流之前作为fsOut打开


fsOut.Write(inputBuffer,0,(int)inputBuffer.Length);

progBar.Value ++;

} // for int i


如您所见,对象fs和inputBuffer都被创建为

" new"每次通过循环,这可能是很多次。我没有b $ b认为这会起作用;我只是试着看看我会得到什么样的错误

消息,当它运行时我很惊讶。每次试运行

都能产生完美的效果。


那么这里发生了什么?内存是否正在被重用,或者我在堆上的对象上堆积了只会在我的程序结束时消失的内容,或者

我是否创建了大量的内存泄漏?


我可以看到在fs.Close()之后fs可能会消失,但我不会理解为什么我可以重新创建字节数组一遍又一遍,

没有处理它。我已经验证了调试器

每次输入文件大小改变时,数组都有不同的大小,

因此每次循环都会重新分配它,而不是

而不仅仅是被重用。我试图找到关于新

如何在循环中起作用的解释,但到目前为止我还没能做到。任何帮助,

包括指向VS文档的指针或一本关于C#的热门书籍,都将获得赞赏。
I''m just learning C#. I''m writing a program (using Visual C# 2005 on
WinXP) to combine several files into one (HKSplit is a popular
freeware program that does this, but it requires all input and output
to be within one directory, and I want to be able to combine files
from different directories into another directory of my choice).

My program seems to work fine, but I''m wondering about this loop:
for (int i = 0; i < numFiles; i++)
{
// read next input file

FileStream fs = new FileStream(fileNames[i],
FileMode.Open, FileAccess.Read, FileShare.Read);
Byte[] inputBuffer = new Byte[fs.Length];

fs.Read(inputBuffer, 0, (int)fs.Length);
fs.Close();

//append to output stream previously opened as fsOut

fsOut.Write(inputBuffer, 0, (int) inputBuffer.Length);
progBar.Value++;
} // for int i

As you can see, the objects fs and inputBuffer are both created as
"new" each time through the loop, which could be many times. I didn''t
think this would work; I just tried it to see what kind of error
message I would get, and I was surprised when it ran. Every test run
has produced perfect results.

So what is happening here? Is the memory being reused, or am I piling
up objects on the heap that will only go away when my program ends, or
am I creating a huge memory leak?

I can see that fs might go away after fs.Close(), but I don''t
understand why I''m allowed to recreate the byte array over and over,
without ever disposing of it. I have verifed with the debugger that
the array has a different size each time the input file size changes,
so it really is being reallocated each time through the loop, rather
than just being reused. I''ve tried to find explanations of how "new"
works in a loop, but I haven''t been able to so far. Any help,
including pointers to the VS docs or a popular book on C#, would be
appreciated.





Tony Sinclair写道:

Tony Sinclair wrote:

我只是在学习C#。我正在编写一个程序(在
WinXP上使用Visual C#2005)将几个文件组合成一个(HKSplit是一个流行的

免费软件程序,它可以做到这一点,但它要求所有输入和输出

都在一个目录中,我希望能够将来自不同目录的文件

组合到我选择的另一个目录中。


我的程序似乎工作正常,但我想知道这个循环:


for(int i = 0; i< numFiles; i ++)

{

//读取下一个输入文件


FileStream fs = new FileStream(fileNames [i],

FileMode.Open,FileAccess.Read,FileShare.Read);

Byte [] inputBuffer = new Byte [fs.Length];


fs.Read(inputBuffer,0,(int)fs.Length);

fs.Close();


//附加到输出流之前作为fsOut打开


fsOut.Write(inputBuffer,0,(int)inputBuffer.Length);

progBar.Value ++;

} // for int i


如您所见,对象fs和inputBuffer都被创建为

" new"每次通过循环,这可能是很多次。我没有b $ b认为这会起作用;我只是试着看看我会得到什么样的错误

消息,当它运行时我很惊讶。每次试运行

都能产生完美的效果。


那么这里发生了什么?内存是否正在被重用,或者我在堆上的对象上堆积了只会在我的程序结束时消失的内容,或者

我是否创建了大量的内存泄漏?
I''m just learning C#. I''m writing a program (using Visual C# 2005 on
WinXP) to combine several files into one (HKSplit is a popular
freeware program that does this, but it requires all input and output
to be within one directory, and I want to be able to combine files
from different directories into another directory of my choice).

My program seems to work fine, but I''m wondering about this loop:
for (int i = 0; i < numFiles; i++)
{
// read next input file

FileStream fs = new FileStream(fileNames[i],
FileMode.Open, FileAccess.Read, FileShare.Read);
Byte[] inputBuffer = new Byte[fs.Length];

fs.Read(inputBuffer, 0, (int)fs.Length);
fs.Close();

//append to output stream previously opened as fsOut

fsOut.Write(inputBuffer, 0, (int) inputBuffer.Length);
progBar.Value++;
} // for int i

As you can see, the objects fs and inputBuffer are both created as
"new" each time through the loop, which could be many times. I didn''t
think this would work; I just tried it to see what kind of error
message I would get, and I was surprised when it ran. Every test run
has produced perfect results.

So what is happening here? Is the memory being reused, or am I piling
up objects on the heap that will only go away when my program ends, or
am I creating a huge memory leak?



不太可能是你在创建内存泄漏。 C#使用垃圾

集合。

当对象超出范围时(在你的情况下,}标记为//为

int i)

对象被销毁。下一次循环,一个新的是

创建。


Matt

Unlikely that you are creating a memory leak. C# uses garbage
collection.
When the object goes out of scope (in your case, the } marked // for
int i)
the object is destroyed. The next time through the loop, a new one is
created.

Matt


这篇关于“新”怎么样?在循环中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆