请问各位专业人士!1、定义一个类,该类中包括一个值类型的数组数据,譬如float[,] _data = new float[1000,1000],该属性在定义就进行了实例化;如下:
public class TestClass
{
float[,] _data = new float[500, 500];
public TestClass()
{
}
public float[,] Data
{
get { return _data; }
set { _data = value; }
}
}
2、实例化该类。
_testClass = new TestClass();
3、在外部,创建一个相同类型的值类型数组;float[,] _newData = new float[5000,5000]; 并将该值赋值给实例化的类;
float[,] _data = new float[1000, 1000];
_testClass.Data = _data;问题是:
这时候的内存占用是一份还是两份呢?如果是一份的话,实例化时所分配的内存是在什么回收掉的?
public class TestClass
{
float[,] _data = new float[500, 500];
public TestClass()
{
}
public float[,] Data
{
get { return _data; }
set { _data = value; }
}
}
2、实例化该类。
_testClass = new TestClass();
3、在外部,创建一个相同类型的值类型数组;float[,] _newData = new float[5000,5000]; 并将该值赋值给实例化的类;
float[,] _data = new float[1000, 1000];
_testClass.Data = _data;问题是:
这时候的内存占用是一份还是两份呢?如果是一份的话,实例化时所分配的内存是在什么回收掉的?
在
_testClass.Data = _data
以后,GC会发现
原来的_testClass.Data没人要了。
所以回收
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;namespace MemoryTest
{
public partial class Form1 : Form
{
PerformanceCounter _mem;
TestClass _testClass;
StringBuilder _sb = new StringBuilder(3 * 1024); //预先开辟一份内存,
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
_testClass = new TestClass();
GC.Collect(GC.MaxGeneration);
WriteLine("Class is created.");
} private void button2_Click(object sender, EventArgs e)
{
if (_mem == null)
{
string _instanceName = System.Reflection.Assembly.GetEntryAssembly().GetName(false).Name;
if (PerformanceCounterCategory.Exists("Process") &&
PerformanceCounterCategory.CounterExists("Working Set", "Process") &&
PerformanceCounterCategory.InstanceExists(_instanceName, "Process"))
{
_mem = new PerformanceCounter("Process", "Working Set", _instanceName);
}
}
if (_mem != null)
{
WriteLine("HashCode:{0} Memory:{1}Kb", _testClass != null ? _testClass.GetHashCode() : 0, _mem.NextValue() / 1024);
}
} private void button3_Click(object sender, EventArgs e)
{
if (_testClass != null)
{
float[,] _data = new float[5000, 5000];
_testClass.Data = _data;
GC.Collect(GC.MaxGeneration);
WriteLine("Class's attribute is set value.");
}
} void WriteLine(string format, params object[] args)
{
_sb.AppendFormat(format, args);
_sb.AppendLine();
//string _message = format;
//if (args != null)
// _message = string.Format(format, args);
//richTextBox1.AppendText(_message);
//richTextBox1.AppendText(Environment.NewLine);
//richTextBox1.ScrollToCaret(); //Console.WriteLine(format, args);
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Console.WriteLine(_sb.ToString());
}
}
public class TestClass
{
float[,] _data = new float[500, 500];
public TestClass()
{
}
public float[,] Data
{
get { return _data; }
set { _data = value; }
}
}
}
的时候,内存里面是两份。当你把这一份赋值给_testClass.Data = _data;
后内存里只有一份,其中一份已被回收,属性和外部的数组都指向一个内存空间
{
float[,] _data外 = new float[1000, 1000];
TestClass _testClass = new TestClass();
_testClass.Data = _data外;
_data外[0, 0] = 100; GC.Collect(0);
;
}
public class TestClass
{
public float[,] _data = new float[500, 500];
public TestClass()
{
}
public float[,] Data
{
get { return _data; }
set { _data = value; }
}
}
所以_testClass.Data = _data;执行完后,内存中还存在块A和块B,块A何时被GC回收,由GC确定
所有的数组都是引用类型