关于C#中数组浅拷贝的问题

发布于 2024-07-17 07:45:01 字数 575 浏览 13 评论 0原文

只是为了确保我正确理解引用类型的浅拷贝,并且我没有在这里构建巨大的内存泄漏:

// Adds text to the beginning of the log RTB
// Also keeps the log RTB trimmed to 100 lines
var lines = new string[rtbLog.Lines.Length + 1];
lines[0] = "text";
Array.Copy(rtbLog.Lines, 0, lines, 1, rtbLog.Lines.Length);

if (lines.Length > 100)
{
    Array.Resize(ref lines, 100);
}

rtbLog.Lines = lines;

这首先会将 rtbLog.Lines 中的字符串的引用复制到行中。 然后它会将 line 中的前 100 个引用复制到一个新的字符串数组中。

这意味着 rtbLog.Lines 最初引用的数组、行最初引用的数组(调整大小之前),以及最后行中不包含的任何字符串(调整大小之后),都​​会被垃圾收集。 (我希望这是有道理的)

对吗?

Just to make sure I'm understanding shallow copies of reference types correctly and that I'm not constructing a huge memory leak here:

// Adds text to the beginning of the log RTB
// Also keeps the log RTB trimmed to 100 lines
var lines = new string[rtbLog.Lines.Length + 1];
lines[0] = "text";
Array.Copy(rtbLog.Lines, 0, lines, 1, rtbLog.Lines.Length);

if (lines.Length > 100)
{
    Array.Resize(ref lines, 100);
}

rtbLog.Lines = lines;

This would firstly copy the refs to the strings in rtbLog.Lines into lines. Then it would copy the first 100 refs from lines into a new string array.

Meaning the array that rtbLog.Lines was originally referencing, the array initially referenced by lines (before the resize), and lastly any strings not contained in lines (after the resize), all get garbage collected. (I hope that makes sense)

Correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2024-07-24 07:45:01

Array.Resize 方法有点用词不当。 它确实应该命名为 CopyToNewArrayWithSize。 在底层,该 API 将创建一个新数组并将指定的数据复制到该数组中。 然后通过引用返回新数组。

至于垃圾收集。 通过将 Lines 属性重置为新数组,您成功删除了对该数组的原始引用。 只要没有其他对该数组的引用,它就会在将来的某个时刻被垃圾回收。

The Array.Resize method is a bit of a misnomer. It should really be named CopyToNewArrayWithSize. Under the hood, this API will create a new array and copy the specified data into that array. The new array is then returned by reference.

As for garbage collection. By reseting the Lines property to the new array, you did successfully remove the original reference to the array. As long as there are no other references to the array, it will be garbage collected at some point in the future.

嘴硬脾气大 2024-07-24 07:45:01

是的,不再使用的原始数组和字符串被垃圾收集就好了。

如果您在创建数组之前计算所需的大小,则不必调用 Resize (因为这将创建数组的另一个副本)。

// Adds text to the beginning of the log RTB
// Also keeps the log RTB trimmed to 100 lines
int size = Math.Min(rtbLog.Lines.Length + 1, 100);
string[] lines = new string[size];
lines[0] = "text";
Array.Copy(rtbLog.Lines, 0, lines, 1, size - 1);
rtbLog.Lines = lines;

Yes, the original array and strings that are not used any more are garbage collected just fine.

If you calculate the size that you want before creating the array, you don't have to call Resize (as that will create yet another copy of the array).

// Adds text to the beginning of the log RTB
// Also keeps the log RTB trimmed to 100 lines
int size = Math.Min(rtbLog.Lines.Length + 1, 100);
string[] lines = new string[size];
lines[0] = "text";
Array.Copy(rtbLog.Lines, 0, lines, 1, size - 1);
rtbLog.Lines = lines;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文