When we create types, what will be the size of the memory?
For example:
Employee myAssistant = new Employee();
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 8, 2014, 10:22 AM
For user-defined types, there is no maximum memory size as such - though the allocation of such objects would of course be limited by available memory.
In practice, the biggest types are generally arrays, lists, dictionaries and strings. If a type requires more than 85K bytes it is stored in a special area of the heap (the large object heap or LOH).
In the normal heap, when objects are deleted, objects higher in memory are moved down to fill up the space and new objects are then allocated at the end.
In contrast, in the LOH, memory is not condensed in this way when objects are deleted. When new large objects are allocated an attempt is first made to allocate them in one of the 'holes' left by previously deleted objects and, if this proves impossible, then they are then allocated at the end of the LOH.
This can sometimes lead to a situation where an out of memory exception occurs even though there appears to be plenty of available memory remaining.
MahaPosted Nov 8, 2014, 10:26 AM
MahaPosted Nov 8, 2014, 10:00 AM
MahaPosted Nov 8, 2014, 9:10 AM
VulpesPosted Nov 8, 2014, 9:07 AM
1. The sum of the memory occupied by each of its fields.
2. Possibly some padding to ensure the fields are aligned on appropriate boundaries for the processor being used.
3, If Employee is a class, overhead of 8 bytes (32 bit processor) or 16 bytes (64 bit processor).
If Employee is a class, then the memory will be allocated on the heap and the variable, myAssistant, will contain a reference (a type of pointer) to this memory. This reference will occupy 4 bytes on a 32 bit processor or 8 bytes on a 64-bit processor.
If Employee is a struct, then the memory may be allocated on the stack or the heap (depending on the circumstances) and the variable myAssistant will contain the actual struct instance, not a reference to it.