Click here to Skip to main content
15,885,366 members
Home / Discussions / C#
   

C#

 
QuestionHow to do code optimization in c# apps Pin
Tridip Bhattacharjee25-Mar-13 9:30
professionalTridip Bhattacharjee25-Mar-13 9:30 
GeneralRe: How to do code optimization in c# apps Pin
harold aptroot25-Mar-13 10:41
harold aptroot25-Mar-13 10:41 
AnswerRe: How to do code optimization in c# apps Pin
jschell26-Mar-13 8:39
jschell26-Mar-13 8:39 
QuestionFew question about static class c# Pin
Tridip Bhattacharjee25-Mar-13 5:01
professionalTridip Bhattacharjee25-Mar-13 5:01 
AnswerRe: Few question about static class c# Pin
PIEBALDconsult25-Mar-13 5:14
mvePIEBALDconsult25-Mar-13 5:14 
AnswerRe: Few question about static class c# Pin
Paulo Zemek25-Mar-13 5:39
mvaPaulo Zemek25-Mar-13 5:39 
GeneralRe: Few question about static class c# Pin
Tridip Bhattacharjee25-Mar-13 8:43
professionalTridip Bhattacharjee25-Mar-13 8:43 
GeneralRe: Few question about static class c# Pin
Paulo Zemek25-Mar-13 9:36
mvaPaulo Zemek25-Mar-13 9:36 
In any method...
C#
int i = 5; // the value 5 is on stack.
MyObject myObject = new MyObject();
// as discussed, the myObject reference is on stack, but the MyObject content is on the heap.
// now, imagine that the MyObject has an int field.
myObject.intField = 10; // as the contents of myObject are on the heap, the intField is on the heap.


If you use unsafe code you can check the real size of objects by using the sizeof() operator. It will show you how many bytes such object will occupy. If it is a value type, that means how many bytes it will occupy where it is declared (if declared locally, it will be in stack... if declared inside another class, that will means how many bytes it will add to the size of instances of that class).

And to explain your question about Name and Salary.
If you have a class with both Name and Salary, the contents of such class will always be on the heap.
So, you will have:
Name = some address... 4 or 8 bytes (depending if the computer is 32 or 64 bits)
Salary = (if it is an int, then 4 bytes).

And that "some address" will in fact be where the "keith" object is contained.
There is where an important factor happens:
Imagine that you have 2 instances of your type that has name and salary, but both pointing to the same name.

Considering a 64 bits computer, each instance will have the normal object header (I think it is 16 bytes) + a string reference (8 bytes) + an int (4 bytes).
That is: 28 for each instance * 2 = 56.
But you will have "keith" only once in memory.
Again, this should be 16 + the length of the string * 2 + some info used by the string itself (I think it is only the hashcode, but I am not sure). That is: 16 + 10 + 4. Or 30 bytes only to contain "keith" in memory.

Now, if you have 100 references to "keith", the string reference will always stay at 8 bytes... so, 100 references = 800 bytes. But the string is only once in memory. Much better than having 30 * 100 (or 3000 bytes) for the strings.
GeneralRe: Few question about static class c# Pin
harold aptroot25-Mar-13 9:50
harold aptroot25-Mar-13 9:50 
AnswerRe: Few question about static class c# Pin
Marco Bertschi25-Mar-13 6:12
protectorMarco Bertschi25-Mar-13 6:12 
GeneralRe: Few question about static class c# Pin
Tridip Bhattacharjee25-Mar-13 8:46
professionalTridip Bhattacharjee25-Mar-13 8:46 
GeneralRe: Few question about static class c# Pin
Marco Bertschi25-Mar-13 9:35
protectorMarco Bertschi25-Mar-13 9:35 
GeneralRe: Few question about static class c# Pin
Tridip Bhattacharjee25-Mar-13 9:53
professionalTridip Bhattacharjee25-Mar-13 9:53 
GeneralRe: Few question about static class c# Pin
Marco Bertschi25-Mar-13 10:16
protectorMarco Bertschi25-Mar-13 10:16 
GeneralRe: Few question about static class c# Pin
Tridip Bhattacharjee25-Mar-13 21:31
professionalTridip Bhattacharjee25-Mar-13 21:31 
GeneralRe: Few question about static class c# Pin
Marco Bertschi25-Mar-13 22:20
protectorMarco Bertschi25-Mar-13 22:20 
AnswerRe: Few question about static class c# Pin
DaveyM6925-Mar-13 9:45
professionalDaveyM6925-Mar-13 9:45 
AnswerRe: Few question about static class c# Pin
V.25-Mar-13 23:04
professionalV.25-Mar-13 23:04 
Questionhow memory is allocated for class, class data member and member function & interface and class instance ? Pin
Tridip Bhattacharjee25-Mar-13 4:59
professionalTridip Bhattacharjee25-Mar-13 4:59 
AnswerRe: how memory is allocated for class, class data member and member function & interface and class instance ? Pin
Paulo Zemek25-Mar-13 5:28
mvaPaulo Zemek25-Mar-13 5:28 
GeneralRe: how memory is allocated for class, class data member and member function & interface and class instance ? Pin
Tridip Bhattacharjee25-Mar-13 9:26
professionalTridip Bhattacharjee25-Mar-13 9:26 
GeneralRe: how memory is allocated for class, class data member and member function & interface and class instance ? Pin
Paulo Zemek25-Mar-13 9:46
mvaPaulo Zemek25-Mar-13 9:46 
QuestionAny C# library for Forward error correction. Pin
iamraghusunkara25-Mar-13 4:33
iamraghusunkara25-Mar-13 4:33 
AnswerRe: Any C# library for Forward error correction. Pin
Marco Bertschi25-Mar-13 6:17
protectorMarco Bertschi25-Mar-13 6:17 
AnswerRe: Any C# library for Forward error correction. Pin
unclepaul25-Mar-13 10:44
unclepaul25-Mar-13 10:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.