Re: Object creation overhead

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/27/03

  • Next message: Anthony Borla: "Re: Object creation overhead"
    Date: Thu, 27 Nov 2003 01:00:13 GMT
    
    

    "nos" <nos@nospam.com> wrote in message
    news:shbxb.316149$Tr4.986273@attbi_s03...
    >
    > please, what is a "proxy" object? how to make one?
    >

    It's just a plain object that is used to represent or take the place of
    another object. While useful in many situations [i.e. there can be many
    reasons for using proxies including security reasons, and minimising
    resources loadtimes etc], one particular use is to reduce the memory
    footprint of a Collection-type of data structure.

    For example, you need to have an 'ArrayList' object manage 1000 objects of
    'MyClass'; this class looks like:

        class MyClass
        {
            ...
            // Lots of fields ...
            private long id;
            private String name;
            private String address;
            ...
        }

    If 'MyClass' has twenty 'String' fields then that means that memory for
    20,000 'String' objects is needed, both to manage them, and for the 'String'
    data. This is, of course, over and above the memory required for the
    'MyClass' objects themselves.

    The 'proxy' class approach won't see a reduction in the number of 'MyClass'
    objects, merely a reduction in the amount of memory each may require for its
    field data. So, the original class is now split into two; there is the proxy
    class:

        class MyClassProxy
        {
            ...
            // Only a couple of fields needed...
            private long id;
            ...
            // Method to retrieve the actual object proxied by this one
            public MyClass getMyClassObject() { ... }
            ...
        }

    and a modified, probably pared-down, version of the original class:

        class MyClass
        {
            ...
            // Still lots of fields, but probably less methods ...
            private long id;
            private String name;
            private String address;
            ...
        }

    With the 'ArrayList' object loaded with 'MyClassProxy' objects, less memory
    is being used. Only when a 'MyClassProxy' object is selected is a 'MyClass'
    object instantiated, using data stored somewhere else *outside memory* [e.g.
    file, database, URL, etc].

    Note:

    * This approach has many variants - I have provided a very simple
       example, and ignored / glossed over the implementation issues.
       However the general approach is:

       - Use proxy object in place of real object

       - When proxy object is selected have it retrieve / create
          the real object

    * Proxy cannot [usually] be used where access / retrieval time is
       crucial

    More substantial examples, and relevant in-depth discussions may be found by
    Google searching for 'Proxy Design Pattern'.

    I hope this helps.

    Anthony Borla


  • Next message: Anthony Borla: "Re: Object creation overhead"

    Relevant Pages

    • Re: Object creation overhead
      ... > class MyClass ... This is, of course, over and above the memory required for the ... > 'MyClass' objects themselves. ... > The 'proxy' class approach won't see a reduction in the number of ...
      (comp.lang.java)
    • Re: C# confusion
      ... If you create instances of MyClass, ... you are allocating an area of memory for the data segment for each ... data members are initialised upon first reference of the class. ... static keyword; This one has been hitting me like a hammer on the ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: C# confusion
      ... MyClass" then you will never create an instance of MyClass on the heap, ... so any instance members you declare in MyClass will never exist. ... of memory: not the stack and not the heap, ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: How to terminate an object thats in a collection
      ... Dim C As Collection ... Dim tmp As Class1 ... My memory is pretty clear on this, but it is not clear on whether there ... Coll.Remove 1 'removes item from collection, but MyClass still exists ...
      (microsoft.public.excel.programming)
    • Re: How to terminate an object thats in a collection
      ... Another way to refer to multiple instances of the same class would be with ... Dim arrClassas Class1 ... Coll.Remove 1 'removes item from collection, but MyClass still ... Removing an object from a collection won't remove it from memory, ...
      (microsoft.public.excel.programming)