How many bytes is taken by an empty class

M

Morgan Cheng

Hi,

I once worked for java in embedded system. Since the memory is limited
in embedded system, there are some guidelines for programming. One of
them is "Don't create too many classes, because each class takes up at
least 200 bytes". The 200 bytes are byte-code, not size of a class
instance.

Since Java and C# is so similar, I am wandering how many bytes will it
take for a C# class after compiled into IL code. If it also consumes
memory, should we avoid creating too many classes?

Thanks,
-Morgan
 
G

Guest

Morgan,
You could certainly decompile your assembly with ILDASM or Reflector and
look at the raw size of the IL code, but that doesn't have a direct relation
to the JIT-ed machine code in memory once the class (assembly) is loaded and
executed by the runtime.

I think for embedded code you would want to be more concerned about not
creating too many *assemblies*, not classes, because it is the assembly that
is the basic unit that gets loaded and executed after JITing.

Peter
 
B

Ben Voigt

Morgan Cheng said:
Hi,

I once worked for java in embedded system. Since the memory is limited
in embedded system, there are some guidelines for programming. One of
them is "Don't create too many classes, because each class takes up at
least 200 bytes". The 200 bytes are byte-code, not size of a class
instance.

Since Java and C# is so similar, I am wandering how many bytes will it
take for a C# class after compiled into IL code. If it also consumes
memory, should we avoid creating too many classes?

Yes, if you're worried about the distributed size of your exe, then by all
means try to minimize the number of classes. This is one of the claimed
advantages of .NET delegates over Java inner classes/adapter pattern. But
in .NET, just about everything carries metadata...

So eliminating thin wrapper methods (which i.e. provide a default value for
one parameter) may save you more space.
 
C

Christof Nordiek

Hi Peter,

Peter Bromberg said:
Morgan,
You could certainly decompile your assembly with ILDASM or Reflector and
look at the raw size of the IL code, but that doesn't have a direct
relation
to the JIT-ed machine code in memory once the class (assembly) is loaded
and
executed by the runtime.

So the ILcode will not stay in memory?
I think for embedded code you would want to be more concerned about not
creating too many *assemblies*, not classes, because it is the assembly
that
is the basic unit that gets loaded and executed after JITing.

IIRC methods not classes or assemblies are JITed

anyway i don't understand why may assemblies are worse than many classes.
 
M

Morgan Cheng

Ben said:
Yes, if you're worried about the distributed size of your exe, then by all
means try to minimize the number of classes. This is one of the claimed
advantages of .NET delegates over Java inner classes/adapter pattern. But
in .NET, just about everything carries metadata...
delegates in C# are compiled into classes as well, right? So, why is it
claimed to have advantages over Java?

In java, one empty class takes about 200 bytes among which only 5 bytes
are bytecode. I checked IL code of a empty class in C# by ILDasm.exe,
the auto-generated default ctor has 7 bytes. I am not sure how many
space its metadat needs.
 
M

Morgan Cheng

Peter said:
Morgan,
You could certainly decompile your assembly with ILDASM or Reflector and
look at the raw size of the IL code, but that doesn't have a direct relation
to the JIT-ed machine code in memory once the class (assembly) is loaded and
executed by the runtime.
With ILDASM.exe, I can only view method size of class member function.
How can I view metadata size of one class?
 
J

Jon Skeet [C# MVP]

delegates in C# are compiled into classes as well, right? So, why is it
claimed to have advantages over Java?

The delegate *type* is only a single type, but *instances* of the type
are created with existing methods.
In java, one empty class takes about 200 bytes among which only 5 bytes
are bytecode. I checked IL code of a empty class in C# by ILDasm.exe,
the auto-generated default ctor has 7 bytes. I am not sure how many
space its metadat needs.

Unless you're in the very tighest of situations, the space taken by a
class shouldn't be a significant factor in the design. What kind of
memory limit do you have?
 
M

Morgan Cheng

Jon 写é“:
The delegate *type* is only a single type, but *instances* of the type
are created with existing methods.
Hm~, below code represents a new class.
delegate void foo();
Unless you're in the very tighest of situations, the space taken by a
class shouldn't be a significant factor in the design. What kind of
memory limit do you have?
In Embedded System.
In Java, if one class takes at least 200 bytes. 5 classes will take 1k.
It is not good. Sometimes, too many anonymous classes is used to
consume valuable memory.
 
J

Jon Skeet [C# MVP]

Morgan Cheng said:
Hm~, below code represents a new class.
delegate void foo();

Yes, and in Java there's be an interface for that. But in Java, each
implementation of that interface might use an anonymous inner class,
where in .NET you wouldn't need a new class, you'd just use a method
within an existing class. You'd create a new *instance* of the delegate
type which knew about the target of the call and the method to call.
In Embedded System.

Is this theoretical or actual though - the use of .NET? I haven't seen
..NET used in the same "tiny memory" situations as Java. Whereas there
are situations where Java is used with only a few K of memory, I
haven't personally seen any uses of .NET in things smaller than a Smart
Phone with significantly more than that. Now, I'm sure there are uses
for .NET which I haven't come across, which is why I'm asking the
question. Even within Smart Phone situations, I'd say memory usually
isn't tight enough to make it worth bending the natural design out of
shape.
In Java, if one class takes at least 200 bytes. 5 classes will take 1k.
It is not good. Sometimes, too many anonymous classes is used to
consume valuable memory.

And those anonymous classes won't exist in .NET unless you're using
..NET 2.0 and anonymous methods, and if those anonymous methods capture
variables.
 
M

Morgan Cheng

Jon said:
Yes, and in Java there's be an interface for that. But in Java, each
implementation of that interface might use an anonymous inner class,
where in .NET you wouldn't need a new class, you'd just use a method
within an existing class. You'd create a new *instance* of the delegate
type which knew about the target of the call and the method to call.

Well, I understand now. Observer Pattern implemented in C#(delegate)
won't get too many classes created.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top