Solved

Instantiating Many Objects (Classes) for Heavy Workloads

Posted on 2013-05-26
11
311 Views
Last Modified: 2013-05-26
I have many classes with which I need to perform heavy computation.  Regarding the classes, all work is performed in the New constructor without need of input parameters.   What would happen to memory and GC if I simply instantiated, for example, 50 objects (classes) and ran them all at the same time?  (Recall, you can't thread an object since it is not a method, and using delegates and reflection is not the way I want to go).   Can I sequentially control the running of objects by using:

Public WorkerBusy As Boolean = False

Do While WorkerBusy = False
  WorkerBusy = True
  Dim obj1 As New WorkClass1
Loop 
Do While WorkerBusy = False
  WorkerBusy = True
  Dim obj2 As New WorkClass2
Loop 
Do While WorkerBusy = False
  WorkerBusy = True
  Dim obj3 As New WorkClass3
Loop 
...
Do While WorkerBusy = False
  WorkerBusy = True
  Dim objp As New WorkClassp
Loop 

Open in new window

At the end of each class you could use:
    .....
    WorkerBusy = False
End Class 

Open in new window

Last, what happens to program flow every time an object is instantiated?  For example, with the following code:
Dim obj1 As New WorkClass1
Dim obj2 As New WorkClass2
Dim obj3 As New WorkClass3
Dim obj4 As New WorkClass4
Dim obj5 As New WorkClass5
Dim obj6 As New WorkClass6
Dim obj7 As New WorkClass7
Dim obj8 As New WorkClass8
Dim obj9 As New WorkClass9
Dim obj10 As New WorkClass10

Open in new window

Does the flow wait until the class is done with the calculations, or does flow start the object (class) and immediately go to the next command?

.NET is working out to be interesting, since when OOP is used, work is split into classes, and GC comes along when the class resources can be released after execution is complete in the class.   However, you can't thread an instantiated object (i.e. Dim obj as New WorkClass), but only methods like subroutines.   There must be a way to instantiate lots of classes for heavy work without reflection and delegates?
0
Comment
Question by:lep1
  • 5
  • 4
  • 2
11 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39197960
Regarding the classes, all work is performed in the New constructor...
Why would you do that? A constructor is for setting up the initial (internal) state of an object, not for processing logic.
0
 
LVL 40
ID: 39197961
Finalization is not deterministic in .NET, which means that there is not way to know when a object is not required. It can lie for hours in the GC before the GC calls its finalizer. The GC does that when its resources pool become too small, not when a variable goes out of scope. So the notion of "At the end of each class" does not exist.

If you call your constructors one after the other as in your last piece of code, as far as I know (this would need to be confirmed by testing) each constructor must finish executing before the following one gets fired.

Now what should you do.

First, check if you really have a performance problem by calling all the constructors one after the other.  If not, then there is nothing to do. It's not the number of objects that has the biggest impace on performance, it is what you do in those constructors.

And although the classes themselves cannot be multithreaded, if you create a method to instantiate each class, you would be able to call these methodes instead.

If all the work is done in the constructor, why do you need a class? Why not have a series of methods that you could multithread if you find out that you need to. That would save a lot of work for the GC, since methods do not need to be garbage collected. And notice that multithread does not always increase performance, specially if there if the user has nothing to do while the code is executing. In such a case, you can even slow down things because switching between threads adds to the execution time. There are exceptions to what follows, but multithreading is mostly useful when you want the user to keep working during a long operation. And in such case, starting a single thread on a method that calls all your constructors would be sufficient.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39197972
@JamesBurger
...specially if there if the user has nothing to do while the code is executing.
I hope the implication there was not that multithreading is only helpful in GUI applications  ; )
0
 
LVL 40
ID: 39198058
@kaufmed

Note the "specially" at the beginning of the sentence. Maybe there is a better word for it in English, but in French (my main langujage), "spécialement" means something like "more often than otherwise".

I felt necessary to make that point because a lot of programmers who have no experience with multithread see it as a magic bullet to solve performance problems, not knowing that in the background, you increase the microprocessor load and sometimes the GC load when using multithread with only performance in mind.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39198070
@JamesBurger

Agreed, but that's most often due to how the multithreaded code was designed, not multithreading itself. Multithreading is, as you know, a very complex topic, and it's very easy to make simple and costly mistakes.

P.S.

I think I read "specially" as "specifically". That is why I posted my previous comment. "(e)specially" makes more sense in that context  : )
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lep1
ID: 39198074
@kaufmed: there is no advantage to calling a subroutine (method) in a class when the work could be done in the constructor as long as the design allows for this.  I do have many classes with their own methods, and these are called when needed.

@jamesburger: I used to code .net using dozens of subroutines in Form1's class.  However, when doing so all of these methods expose their parameters to memory.  On the other hand, large work subroutines (comprised of matrix algebra and not I/O or db access) converted to a class won't expose anything until instantiated.   Also, I was not aware that a method (subroutine) was disposed like an object (instantiated class) when processing has completed in the subroutine.  Basically, OOP dictates that you don't use dozens and dozens of large methods in Form1's class, since the impact on memory is exceedingly large.  Finally, do not vendors if optimization code offer various procedures such as matrix multiply, matrix invert, Cholesky factorization, etc in different classes? For large matrices, any of these classes can require a lot of run time.  Note they these groups don't provide subroutines, but rather require you to instantiate their classes as objects.

Code sitting in a a lot of different classes has no impact when not being used.  However, code in a lot of subroutines in the same class has a greater impact.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39198113
there is no advantage to calling a subroutine (method) in a class when the work could be done in the constructor as long as the design allows for this.
I'm sorry, but you are bastardizing OOP. What advantage are you gaining by putting the logic in the constructor? You don't have to type out one more method call?

Putting logic in the constructor is unexpected. As I mentioned above, a constructor is intended only to initiate an object's state. This is taught in any basic OOP class. By putting the logic in the constructor you have now introduced something that goes against what any student of OOP has been taught. How would anyone else coming behind you know that all of the logic of the class is contained within the constructor, and not within one or more methods (as they were taught)? Yes, functionally you can put the logic in the contstructor and still have the code work as expected (in terms of a result). Does that make it right? I could go to the supermarket right now and punch 5 random people in the face. Does that mean I should?

You've already been given and advantage as to why you would want to use methods:  You can invoke them on other threads. You can also extend or override the functionality of methods--something you cannot do with constructors. If you go about naming your methods properly, you can accurately convey what invoking the method is doing to the state of your object. When I invoke a constructor, I expect that an object is in an initial state, and that I can now modify that state by invoking methods of that object.

Here's an example. Let's say I create myself in code:

Public Class kaufmed
End Class

Open in new window


Now, let's add that I can run 5 miles. Before I add that, though, how should I add this. If I do your approach, then I would have:

Public Class kaufmed
    Private Property _distance As Integer

    Public Sub New()
        Me._distance = 5
    End Sub
End Class

Open in new window


So, how do I run 10 miles?

If we have a method, it's expected that at object initialization I haven't run anywhere:

Public Class kaufmed
    Public Property Distance As Integer

    Public Sub New()
        Me.Distance = 0
    End Sub

    Public Sub Run()
        Me.Distance += 5
    End Sub
End Class

Module Program
    Sub Main()
        Dim k As New kaufmed()

        Console.WriteLine(k.Distance)  ' Prints 0
    End Sub
End Module

Open in new window


...and that after I run one time, I have run 5 miles:

Module Program
    Sub Main()
        Dim k As New kaufmed()

        k.Run()
        Console.WriteLine(k.Distance)  ' Prints 5
    End Sub
End Module

Open in new window


If I run twice, I have run 10 miles:

Module Program
    Sub Main()
        Dim k As New kaufmed()

        k.Run()
        Console.WriteLine(k.Distance)  ' Prints 5
        k.Run()
        Console.WriteLine(k.Distance)  ' Prints 10
    End Sub
End Module

Open in new window


Code sitting in a a lot of different classes has no impact when not being used.
I get the feeling that you are thinking like a procedural programmer. This is something you are going to have to change in order to be successful with OOP. There are countless classes within the Framework itself that have methods I have never used. That doesn't mean someone else wouldn't use them. It depends on what the task at hand is.
0
 
LVL 40
ID: 39198133
First, your answer to kaufmed is wrong and shows a misunderstanding of what a constructor does. Basically, a constructor is a method, so it behave just like a method, but a constructor also means an instance, an instance is a block of memory. And however small that block of memory is for a class that has only methods, an instance means memory collection. There is no reason to create a class with a constructor when you have only methods in it. This type of thing is usually handled through a static/shared class, a class that you do not instantiate, a module in VB parlance. Could you point me to any of the 15000 classes in the framework that has a constructor but no Public properties? There is an advantage to calling a method instead of a constructor.

Where did I say "that a method (subroutine) was disposed like an object"? When first called, a methods first have a last bit of compile (just-in-time compilation) and is then loaded in memory. Afterward it is kept in cache memory in case you need to reuse it. It is destroyed when the application shuts down. And this is managed by the execution environment of the framework, it has nothing to do with the GC that handles instances, that is the data (member variables) in individual classes.

Having "dozens and dozens of large methods" is not worst than having dozens and dozens of instantiated objects containing similar methods (a constructor is a method) loaded simultaneously in memory. In fact, it is is probably better because the GC has to care about only one big object, the Form itself, instead of the many small objects that you instantiate when you create their individual constructor. And the Form would be in memory anyway since you seem to be working in a Windows application.

As for the way you math libraries are designed, they were probably written in C with some Assembler, or at least for COM/ActiveX in order to enable the use of pointers. Using pointers can improve speed in complex operations, and since pointers are not available in .NET, most libraries that need a lot of performance have not been converted to .NET.

Memory handling in .NET is not the same as in C or in COM. There is no garbage collector in those. The programmer has control over how and when memory is allocated, which is not the case in .NET. You cannot thus assume that a programming approach that is the best in C or COM code will be appropriate in .NET, quite the reverse some times.

And libraries such as MatLab are designed to work with different environments and programming languages. In order to be able to expose their methods to many different types of callers, they are not always developed with usual OOP principles. Most are not event OOP libraries. I have to admit that I have not work with maths for the last 15-20 years. But way back, we did not go directly to MatLab because it was not object oriented. We needed to go through an extension that provided us with a Matrix class that had Add, Substract, Multiply and Invert methods. So what you see of your math library might not be how the library is designed, but how the bridge that you use between .NET and the library is designed.

I do not understand what you mean by "all of these methods expose their parameters to memory". A form is a class, so the level of exposure in a Form is the same as in a class. Unless you declare your variables as global variables or Public at the Form level (Declaratiosn section), the only methods that see a parameter are the caller and the method that is called.

Only trying to help here.
0
 
LVL 40
ID: 39198152
I completely concur with kaufmed, who was writing his answer as I was writing my previous one.

And note that kaufmed has a property in his class. The role of is class is not only to make calculations, but also to hold that property in memory in case further calculations have to be made in the same instance. That is OOP.

Having a class is not the prerequisite for OOP. Data is. If you have only methods and do not hold data between uses of the instance, you are not doing OOP. You simply have a module thas is used as if it was a class. And if you happen to be in Visual Studio 2012, you can create these methods as Async methods, which is an easy way doing multithread.

From what we see from what you say, what you might need is something like the following, a static class:

Public Class WorkClass

   Public Shared Sub YourOperation1 (param As Something)
       'Do something with the param
   End Sub

    Public Shared Sub YourOperation2 (param As Something)
       'Do something with the param
   End Sub

End Class

Open in new window


When you need to use your methods, you simply go:

WorkClass.YourOperation1 (someParam)
WorkClass.YourOperation2 (someParam)

Open in new window


No instanciation, no memory footprint with the GC. No need to create many classes, everything is done in the same one. You can call sequentially, or you can multithread if you want. You have the choice. If you happen to be in Visual Studio 2012, declare these methods Async. The multithread is almost done for you if you really need it.

And this class, it's a module. Modules do not exist in .NET. They are in VB because it eases the transition from VB6 to VB.NET. But when you compile a module, you end up with a static class.
0
 

Author Closing Comment

by:lep1
ID: 39198203
Thanks for the instructive comments.   I liked JB's responses best, primarily since the *explicit* offerings could be more directly applied to the initial issue.  Please tell the site admins that I wanted to score 150 points to KM and 350 to JB.
0
 
LVL 40
ID: 39198395
You are the one that should communicate with the site admins, by clicking the "Request attention" under your original post.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now