473,466 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

many new instances

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?
Apr 6 '06 #1
5 1201
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.

Apr 6 '06 #2
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...?

Apr 6 '06 #3
> 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.
Apr 6 '06 #4
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.

Apr 6 '06 #5
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.

Apr 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
2
by: davidw | last post by:
I want to check chose performance related parameters in my code. Thanks!
11
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
7
by: SamSpade | last post by:
If I define a variable in a module there is only one instance of the variable for all instances of the class. If I define a variable within a class there is an instance of the variable for each...
10
by: Alan G Isaac | last post by:
My class MyClass reuses many default parameters with a small number of changes in each instance. For various reasons I decided to put all the parameters in a separate Params class, instances of...
4
by: =?Utf-8?B?VzFsZDBuZTc0?= | last post by:
When one architects a new project one of the first steps in the decision is to decide on the layers. (In my opinion anyway) One architecture that I have used before is to go solid OO and create...
9
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
3
by: sklett | last post by:
I suspect the answer might be in one of the words of my subject, but here goes anyway. I'm working on a system that will execute a very long (300+) chain of task objects. Here is some quick...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.