Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
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?


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).
Henry Minute 16-Oct-10 18:22pm    
The code button is (or should be) at the top of the text entry edit box. You should see (B)old (I)talic (U)nderline (S)trike-out (small) (BIG) (code block) etc. That's the one. I'm on FF too and they are there for me.
OriginalGriff 17-Oct-10 4:39am    
I am amazed that you can get any .NET framework working in 16Kb of RAM!
I would not be looking at C# for this: C or C++ (preferrably the Embedded C++ subset) would be where I would start.
Let us know if you get C# working in so little RAM!
Derberos 17-Oct-10 6:52am    
It is a .net version made for the card. I'm implementing a secure web server which runs on the smart card. Using TLS1.0 with RSA key exchange, triple des encryption and SHA1 hash. The handshake is done. Just having a problem with the application data. I kinda need 12+Kb of ram which I don't have.
If I could I would have done it in C but my Java and C# was the available options.
Will post the code in an article or something when I'm done.
GPUToaster™ 17-Oct-10 10:18am    
considering your specification, it is really hard to believe that a webserver with RSA key exchange and triple DES encryption and SHA1 hash feature could exist on such a small piece of memory. We would like if you could share your experience with, because the work you are doing could be a great use to security softwares. Best luck with your work.
Derberos 17-Oct-10 11:21am    
I'm changing the triple des to RC4. Hopefully that will speed up the process. There has been a lot of research regarding this. Just a note: It's a real minimal functionality web server.

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.
Share this answer
Dalek Dave 20-Oct-10 3:42am    
Good answer.
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.
Share this answer
Sergey Alexandrovich Kryukov 18-Dec-10 19:06pm    
Very questionable assertion. More importantly, this answer has nothing to do with the question. For your information, a class (in this scope) DOES NOT MEAN OOP (surprize?!) and struct does not mean the opposite to OOP. The difference is reference vs value types, that's it.
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.
Share this answer
Espen Harlinn 26-Feb-11 10:57am    
Good points, my 5
Sergey Alexandrovich Kryukov 26-Feb-11 20:02pm    
Thank you.
Somewhat controversial discussion. In real life it depends on whatsla and howsla... (do you understand this allusion?)
_Ashish 28-Feb-11 13:19pm    
In real life, it mostly depends on your design and on what kind of economy is more critical to your purpose.

I agree to this.. +5
Sergey Alexandrovich Kryukov 1-Mar-11 3:30am    
Ye.., thank you, Ashish,
The solution was to use procedural programming. Structs was used to group data together that belonged together.

I will write a complete article on my solution when I get the time.

Thanks for the good answers.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900