• Class property object in Constructor?

    Ganesh Member

    I can’t really find a nice article, but this is actually the logical thing to do in applications… think of a class with 1000 functions (hypothetical)… and 10 properties. If I create 1000 instances of that class… WHY should the functions be copied around? We would be wasting memory. There are reasons why you can only have on version of an assembly per application domain. All the functions/methods are stored in a separate area from the data they operate on. The actual details get technical with virtual function tables and how the compiler handles things like “this” and “me” and so on. In memory organisation, you have concepts like the stack, the heap, static vs instance variables, allocation tables, pointers and so on. Managed code hides these details away from us… but deep down, that is still how programming languages work.

    These might help:

    http://stackoverflow.com/questions/1298122/where-are-methods-stored-in-memory
    http://stackoverflow.com/questions/18744553/when-instantiating-an-object-does-it-all-get-stored-in-memory
    http://www.beingdeveloper.com/memory-layout-of-a-program/
    https://en.wikipedia.org/wiki/Virtual_method_table
    
  • Amit Member

    Unless this class holds a large amount of data it really should not matter so long as it is instantiated before you try to use it. Given this, creating the instance in the constructor would be the best way to ensure that the class is available when needed, otherwise if you use this class in several different methods you would need to check if the class has been instantiated and then create it if needed causing you to duplicate code

  • Adan Member

    The original class with those attributes and method code is a total of 368 lines of code. Every user running this web application would only have one instance of this class object. However, let say if 20 users using this then 20 class objects would be stored in server memory, is that still okay?

  • Ganesh Member

    As you also asked where you want to instantiate this model, it depend on your requirement. Please read the page life cycle and understand then you can easily decide yourself as per your requirement.

    https://msdn.microsoft.com/en-us/library/ms178472.aspx
  • Amit Member

    “Session memory but now I’m thinking I should only put in session those public attributes that need to retain the values and not the methods code.”

    I’m a bit concerned with that statement… In terms of memory usage, your methods and code are not sitting in the session variable at all. Only a reference to the object. The methods are not copied along with each instance. the methods remain in the in-memory copy of the DLL (on copy for the whole application).

  • Adan Member

    if I instantiate the original class which has all these attributes and 5,6 methods in this class object, when I put this class object into session memory to save it so I don’t lose the attributes values when page posts back. You’re saying the session memory will only save the attributes values part of the class object? wow, never knew that before. Is there an article that you can point me to?

Viewing 5 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish