c#里面如何实时的真测一个微信小程序实时音视频的内存消耗

& & 今日,在项目重构的时候忽然想到一个问题,一个类哪些成员的增加,会影响一个类所占内存的大小?C#有没有办法知道一个对象占多少内存呢?
& & &第一个问题:很快想到是类的非静态的字段、属性。
& & &第二个问题:首先想到的是sizeof()。
下面开始验证,首先来验证值类型,验证代码如下:
int size = sizeof (int); //4个字节
注意点:sizeof&运算符仅适用于值类型,而不适用于引用类型。sizeof&运算符只能在不安全代码块中使用。如下面的代码将无法编译通过:
public struct TestStuct
int size = sizeof(new TestStuct());
编译后,提示:
错误 1 &ConsoleApplication3.TestStuct&没有预定义的大小,因此 sizeof 只能在不安全的上下文中使用(请考虑使用 System.Runtime.InteropServices.Marshal.SizeOf)&
修改为Marshal.SizeOf方法,改方法返回对象的非托管大小(以字节为单位)。参数可以是引用类型或装箱的值类型。布局必须是连续的或显式的。
int size = Marshal.SizeOf(new TestStuct()); //1个字节
接下来来验证引用类型:
由于不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量。所有下面的代码在运行的时候,会抛出异常。
public class Student
int size = Marshal.SizeOf(new Student());
需要给Student类,加上一个StructLayoutAttribute,来控制Student类的数据字段的物理布局。修改代码为:
[StructLayout(LayoutKind.Sequential)]
public class Student
int size = Marshal.SizeOf(new Student()); //1个字节
LayoutKind 默认值为Auto.结论:1:对于托管对象是没有办法直接获取到一个对象所占的内存大小。2:非托管对象,可以使用Marshal.SizeOf3:对内置类型,如int,long,byte等使用sizeof扩展:有人提出使用二进制序列化,将一个对象序列化成一个MemoryStream,然后返回MemoryStream.Length,经过验证是不可以的。验证代码如下:
[Serializable]
public class Student
private static long GetObjectSize(object o)
using (var stream = new MemoryStream())
var formatter = new BinaryFormatter();
formatter.Serialize(stream, o);
using (var fileStream = new FileStream(@"D:\Student.txt", FileMode.OpenOrCreate, FileAccess.Write))
var buffer = stream.ToArray();
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Flush();
return stream.L
var student = new Student();
long size = GetObjectSize(student);
//139个字节
Student.txt保存的文本信息如下所示,通过文本信息,可以得知多出来的100多个字节,估计是就是这一串字符串吧。
JConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
ConsoleApplication3.Student
延伸阅读:原文如下:
We don't expose the managed size of objects because we want to reserve the ability to change the way we lay these things out.&&For example, on some systems we might align and pack differently.&&For this to happen, you need to specify tdAutoLayout for the layout mask of your ValueType or Class.&&If you specify tdExplicitLayout or tdSequentialLayout, the CLR&s freedom to optimize your layout is constrained.
If you are curious to know how big an object happens to be, there are a variety of ways to discover this.&You can look in the debugger.&&For example, Strike or SOS (son-of-strike) shows you how objects are laid out.&&Or you could allocate two objects and then use unverifiable operations to subtract the addresses.&99.9% of the time, the two objects will be adjacent.&&You can also use a managed&profiler&to get a sense of how much memory is consumed by instances of a particular type.
But we don't want to provide an API, because then you could form a dependency over this implementation detail.
Some people have confused the System.Runtime.InteropServices.Marshal.SizeOf() service with this API.&However, Marshal.SizeOf reveals the size of an object after it has been marshaled.&&In other words, it yields the size of the object when converted to an unmanaged representation.&&These sizes will certainly differ if the CLR&s loader has re-ordered small fields so they can be packed together on a tdAutoLayout type.
阅读(...) 评论()c# 程序内存占用过大 解决方法!
[问题点数:20分,结帖人zxvyio]
本版专家分:0
结帖率 97.83%
CSDN今日推荐
本版专家分:721
本版专家分:88513
2017年 总版技术专家分年内排行榜第五
2017年7月 总版技术专家分月排行榜第二
2017年6月 总版技术专家分月排行榜第三
2017年12月 .NET技术大版内专家分月排行榜第一2017年11月 .NET技术大版内专家分月排行榜第一2017年10月 .NET技术大版内专家分月排行榜第一2017年9月 .NET技术大版内专家分月排行榜第一2017年8月 多媒体开发大版内专家分月排行榜第一2017年8月 .NET技术大版内专家分月排行榜第一2017年7月 多媒体开发大版内专家分月排行榜第一2017年7月 .NET技术大版内专家分月排行榜第一2017年6月 .NET技术大版内专家分月排行榜第一2017年5月 多媒体开发大版内专家分月排行榜第一2017年4月 多媒体开发大版内专家分月排行榜第一2016年1月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年11月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年10月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年9月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年7月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年6月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年3月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年2月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2015年1月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年12月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年11月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年10月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年9月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年8月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一2014年7月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第一
本版专家分:0
结帖率 97.83%
本版专家分:448722
2017年 总版技术专家分年内排行榜第十2013年 总版技术专家分年内排行榜第八
2017年2月 总版技术专家分月排行榜第三
2018年6月 .NET技术大版内专家分月排行榜第一2018年1月 .NET技术大版内专家分月排行榜第一2017年5月 .NET技术大版内专家分月排行榜第一2017年4月 .NET技术大版内专家分月排行榜第一2017年3月 .NET技术大版内专家分月排行榜第一2017年2月 .NET技术大版内专家分月排行榜第一2016年10月 .NET技术大版内专家分月排行榜第一2016年8月 .NET技术大版内专家分月排行榜第一2016年7月 .NET技术大版内专家分月排行榜第一
2018年4月 .NET技术大版内专家分月排行榜第二2018年3月 .NET技术大版内专家分月排行榜第二2017年12月 .NET技术大版内专家分月排行榜第二2017年9月 .NET技术大版内专家分月排行榜第二2017年7月 .NET技术大版内专家分月排行榜第二2017年6月 .NET技术大版内专家分月排行榜第二2016年12月 .NET技术大版内专家分月排行榜第二2016年9月 .NET技术大版内专家分月排行榜第二2016年6月 .NET技术大版内专家分月排行榜第二2016年3月 .NET技术大版内专家分月排行榜第二2016年1月 .NET技术大版内专家分月排行榜第二2015年12月 .NET技术大版内专家分月排行榜第二2015年2月 .NET技术大版内专家分月排行榜第二2015年1月 .NET技术大版内专家分月排行榜第二2014年11月 .NET技术大版内专家分月排行榜第二2014年5月 .NET技术大版内专家分月排行榜第二2014年4月 .NET技术大版内专家分月排行榜第二2012年2月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第二
本版专家分:448722
2017年 总版技术专家分年内排行榜第十2013年 总版技术专家分年内排行榜第八
2017年2月 总版技术专家分月排行榜第三
2018年6月 .NET技术大版内专家分月排行榜第一2018年1月 .NET技术大版内专家分月排行榜第一2017年5月 .NET技术大版内专家分月排行榜第一2017年4月 .NET技术大版内专家分月排行榜第一2017年3月 .NET技术大版内专家分月排行榜第一2017年2月 .NET技术大版内专家分月排行榜第一2016年10月 .NET技术大版内专家分月排行榜第一2016年8月 .NET技术大版内专家分月排行榜第一2016年7月 .NET技术大版内专家分月排行榜第一
2018年4月 .NET技术大版内专家分月排行榜第二2018年3月 .NET技术大版内专家分月排行榜第二2017年12月 .NET技术大版内专家分月排行榜第二2017年9月 .NET技术大版内专家分月排行榜第二2017年7月 .NET技术大版内专家分月排行榜第二2017年6月 .NET技术大版内专家分月排行榜第二2016年12月 .NET技术大版内专家分月排行榜第二2016年9月 .NET技术大版内专家分月排行榜第二2016年6月 .NET技术大版内专家分月排行榜第二2016年3月 .NET技术大版内专家分月排行榜第二2016年1月 .NET技术大版内专家分月排行榜第二2015年12月 .NET技术大版内专家分月排行榜第二2015年2月 .NET技术大版内专家分月排行榜第二2015年1月 .NET技术大版内专家分月排行榜第二2014年11月 .NET技术大版内专家分月排行榜第二2014年5月 .NET技术大版内专家分月排行榜第二2014年4月 .NET技术大版内专家分月排行榜第二2012年2月 多媒体/设计/Flash/Silverlight 开发大版内专家分月排行榜第二
本版专家分:0
结帖率 97.83%
本版专家分:5724
本版专家分:189
本版专家分:16
结帖率 96.55%
本版专家分:13708
本版专家分:0
结帖率 97.83%
匿名用户不能发表回复!|
CSDN今日推荐怎么测试一个java程序占用的内存和cpu消耗_百度知道
怎么测试一个java程序占用的内存和cpu消耗
我有更好的答案
只能查看JAVA 虚拟机占用的内存Runtime.getRuntime().maxMemory() 返回 Java 虚拟机试图使用的最大内存量。Runtime.getRuntime().freeMemory() 返回 Java 虚拟机中的空闲内存量。Runtime.getRuntime().totalMemory()
返回 Java 虚拟机中的内存总量。
采纳率:98%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。C语言——内存消耗测试程序
/*这是一个消耗内存的测试c程序*/
./test-mem arg1 arg2 arg3
* arg1表示每秒分配的内存数量(单位M)
* arg2表示总共分配多少内存
* arg3表示程序存活多久(默认300秒)
# include &stdio.h&
# include &stdlib.h&
# include &string.h&
# include &unistd.h&
# define KB (1024)
# define MB (1024 * KB)
# define GB (1024 * MB)
int main(int argc, char *argv[])
int size = 0, i = 0, j = 0;
size = atoi(argv[1]);
int end_time=300;
int total_size = -1;
if (argc & 2) {
total_size = atoi(argv[2]);
if (size & total_size)
total_size =
if (argc & 3) {
end_time = atoi(argv[3]);
while ((p = (char *)malloc(GB)))
memset(p, 0, GB);
while (1) {
if(total_size != j) {
while (i--) {
p = (char *)malloc(MB);
memset(p, 0, MB);
printf("malloc: %dMB\r", j);
fflush(stdout);
end_time--;
if(end_time == 0) {
while ((p = (char *)malloc(KB)))
memset(p, 0, KB);
//sleep(1);
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 微信小程序实时音视频 的文章

 

随机推荐