内存效率:对象与结构 [英] Memory efficiency: Object vs struct

查看:44
本文介绍了内存效率:对象与结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.


只是快速了解问题的背景.我正在使用精简的.net框架和非常有限的资源(16Kb的ram)在智能卡上开发应用程序.我设计并开发了使用面向对象的解决方案,但是该解决方案占用了大量内存.返回并重构代码以尽可能减少内存消耗,但仍然占用大量内存.

现在,我正在使用一种过程方法,并使用struct封装数据.我在这种方法上取得了成功.

这听起来很愚蠢,因为struct解决方案正在工作.现在的问题是:这是资源约束设备的最佳解决方案,还是我的面向对象的解决方案设计不当?然后另一件事我该如何减小exe大小?不确定会影响exe大小吗?

谢谢

只是一个离题的问题.在哪里可以阅读如何设置帖子格式?喜欢如何插入代码块?有人告诉我,我应该选择代码,然后单击代码"按钮,但我看不到该按钮.如果它可以帮助我使用firefox(64bit).

Hey guys.


Just a quick background on the problem. I''m developing an application on a smart card with a reduced .net framework and very limited resources (16Kb of ram). I designed and developed a solution using object orientation but this solution uses a lot of memory. Went back and refactored the code to reduce memory consumption where possible but still it used to much memory.

Now I''m using a procedural approach and using struct to encapsulate data. I''m having success with this approach.

This may sound silly because the struct solution is working. Now the question is: is this the best solution for resource constraint devices or was there my object orientated solution poorly designed? Then another thing how can I reduce the exe size? Not sure what impacts the exe size?

Thanks

Just an off topic question. Where can I read how to format my posts? Like how to insert code block? Someone told me that I should select the code an click on the code button but I can''t see that button. If it may help I''m using firefox(64bit).

推荐答案

托管代码会占用大量资源.有了有限的资源,您应该将C ++与尽可能少的C ++/CLI,MFC和.NET一起使用.性能也更好...

当然,在这种大小的可执行文件上可以存在DES加密.并不是那么困难;我分析了10年前在大学中如何破解DES.您只需要说明并精简可执行文件...

我不认为使用C ++而不是C不会产生巨大的额外开销,但是我仍然建议您不要使用new,如果您不希望因内存不足而抛出异常.无论如何,Malloc和calloc都更有趣.
Managed code is resource hungry. With that limited resources, you should use C++ with as little C++/CLI, MFC, and .NET as possible. Performance is better too...

Of course DES encryption could exist on that size executable. It''s not that difficult; I analyzed how to break DES 10 years ago in college. You just need to mean and lean executables...

I do not think there is a huge additional overhead to using C++ instead of C, but I still would suggest that you do not use new if you do not want thrown exceptions for out of memory. Malloc and calloc are much more fun anyway.


面向对象编程不是万能的.过程编程对于中小型项目非常有效.
100000代码行的非常粗略的估算可以认为是这两种编程范例之间的临界点.
:)
Object Oriented Programming is not a panacea. Procedural programming is very effective for small (and medium) sized projects.
A very rough estimation of 100000 lines of code could be assumed as the cut-off between the two programming paradigms.
:)


以前的答案绝对是不对的.

首先,结构与类之间并没有真正定义可执行文件的大小(可执行文件的大小始终存在差异;但这更多地取决于您使用它的方式;这只是代码量).通常,更多的代码量将导致更大的可执行文件大小.特别是,如果您使用更高级的功能,则可以节省可执行文件的大小,而不用等待它,因为与您自己的可执行文件中驻留的代码相比,您更多地依赖于.NET Framework代码.例如,您可以节省OOP的方法是,您的设计本质上是基于继承和后期绑定的,从而提高了代码重用性.这样可以潜在地节省课程.如果您仅使用类来使用类,而没有充分利用OOP的好处,则与结构相比,您只能在磁盘上释放一些空间.

现在,您的问题是关于节省内存.首先,与结构相比,类的开销更大.但这还不是故事的结局.更重要(且更易于考虑)的是引用类型(例如类实例)和值类型(例如结构实例)之间的区别.首先,由于引用类型在堆上运行,因此会产生少量堆开销.但是,与值类型相比,引用类型可以节省堆栈内存,而每次通过值传递实例时,引用类型都可以在堆栈上克隆额外的内存;与传递引用类型相比,它还需要更多的CPU时间.但是,您可以通过简单地广泛使用by-reference函数参数来避免这些多个克隆.这种方法将为您带来最大的内存经济性.另外,堆栈内存会限制您吗?

最后:这就是您要考虑的全部.在现实生活中,这主要取决于您的设计以及哪种经济对您的目标更为关键.最后的建议是:除非做一些实验,否则您无法做出决定.
Previous answers absolutely miss the point.

First of all, structure vs. class does not really define executable size (there is always difference in executable size; but it depends more on how you use it; this is just the volume of the code). Generally, more volume of code makes more executable size. In particular, if you use more advanced feature, you may save executable size, not waits it, because you more depend on .NET Framework code than on the code which reside in your own executable files. For example, you can save on OOP method is your design essentially based on inheritance and late binding the way it improves the code reuse. This is how you could potentially save on classes. If you use classes just for using classes, not take deeper benefits of OOP, you may only loose some room on your disk compared to structures.

Now, your question was about saving memory. First, there is more overhead of classes compared to structures. But this is not end of story. More important (and easier to take into account) is the difference between reference types such as class instances and value types such as instances of structures. First, as reference types operate on heap, there is a little heap overhead. But reference types save stack memory, compared to value types which will make extra memory clone on stack every time the instance is passed by value; it also takes more CPU time compared to passing reference type. However, you can avoid those multiple clones by simply extensive use of by-reference function arguments. This method will gives you the most of memory economy. Also, does stack memory limit you?

Finally: this is all you have to take into account. In real life, it mostly depends on your design and on what kind of economy is more critical to your purpose. The last advice is: you cannot make your decisions unless you do some experiments.


这篇关于内存效率:对象与结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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