By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

How many bytes is taken by an empty class

P: n/a
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

Sep 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Morgan Cheng" wrote:
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

Sep 11 '06 #2

P: n/a

"Morgan Cheng" <mo************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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.
>
Thanks,
-Morgan

Sep 11 '06 #3

P: n/a
Hi Peter,

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comschrieb im
Newsbeitrag news:15**********************************@microsof t.com...
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.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Morgan Cheng" wrote:
>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


Sep 11 '06 #4

P: n/a

Ben Voigt wrote:
"Morgan Cheng" <mo************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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...
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.
So eliminating thin wrapper methods (which i.e. provide a default value for
one parameter) may save you more space.

Thanks,
-Morgan
Sep 12 '06 #5

P: n/a

Peter wrote:
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?
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Morgan Cheng" wrote:
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
Sep 12 '06 #6

P: n/a
Morgan Cheng <mo************@gmail.comwrote:
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?
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #7

P: n/a

Jon 写道:
Morgan Cheng <mo************@gmail.comwrote:
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?

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();
>
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?
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.
>
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #8

P: n/a
Morgan Cheng <mo************@gmail.comwrote:
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.
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 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?
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #9

P: n/a

Jon wrote:
Morgan Cheng <mo************@gmail.comwrote:
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.
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.
Well, I understand now. Observer Pattern implemented in C#(delegate)
won't get too many classes created.
>
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?
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.