many new instances

M

Martijn Mulder

I do some hefty computing in the inner loop of a graphics application. In
Java I used the new operator a lot since, as many authors assured me, the
Java run-time would implement that very efficient with inline code that
required little overhead.

Does the JIT-compiler optimize my code accordingly?

Since I use the new operator a zillion times in my code, does it mean that
there are a zillion objects created and a zillion instances garbage
collected, or does the compiler optimize that overhead away?
 
N

Natrajk

Object creation is fast in .Net ( as in Java) since the VM always
knows where to put the next object in memory. Since objects created
within loops typically are short lived they will be collected from
generation 0 which is a lot faster than collecting long lived objects.

If you want to keep your object stack allocated you can implement a
struct instead of a class. You'll probably have to do some profiling to
see if this makes your code faster.
 
B

Bruce Wood

What are you instantiating? The "new" operator doesn't always create a
new object on the heap... it depends upon whether you are instantiating
a class or a struct.

Are you instantiating one of your own objects, or are they Points?
Rectangles? Or something else...?
 
M

Martijn Mulder

What are you instantiating? The "new" operator doesn't always create a
new object on the heap... it depends upon whether you are instantiating
a class or a struct.

Are you instantiating one of your own objects, or are they Points?
Rectangles? Or something else...?


I am instantiating many many

new System.Drawing.Drawing2D.Matrix()

-objects with a

new System.Drawing.Rectangle()

object as argument-1and a

new System.Drawing.Point[]
{
new System.Drawing.Point(),
new System.Drawing.Point(),
new System.Drawing.Point()
}

as argument-2. So this one instance already uses 'new' 6 times. They are
anonymous
instances wrapped in a method's argument list.
 
B

Bruce Wood

The only objects you're creating on the heap are Matrix objects.

Rectangle and Point are structs, which means that they are treated like
values, like int, double, etc. If you are passing them as arguments
then "new" will build them directly onto the stack before the
constuctor call to Matrix. After the Matrix object is constucted they
will be popped off the stack. No garbage collection required for them.

It's unlikely that what you're doing will have significant performance
implications, but then that all depends upon how many Matrix instances
you're creating and how fast you instantiate them and throw them away.
Again, the "new"s of Points and Rectangles don't matter... just Matrix.
 
B

Bruce Wood

Oops... I read your post too fast.

There is one other instance being created here: the array of points.
The array itself will be an object allocated on the heap, and will have
to be garbage collected. The individual points will not.

So, that's two objects for every Matrix: one for the point array, and
one for the Matrix itself.
 

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