5

Suppose multiple objects of the same class should do something which takes a memory consuming resource. Which of the following approaches is recommended to define and use the resource by the objects?

  • Each object create the resource in its own
  • Declare one resource instance and pass it to each object
  • Using a static object for the resource

Let me give an example

class Section 
{
     public Bitmap Page {/* set and get the page...*/}
     Bitmap page = new Bitmap(600,800);
     Draw(SomeThing s) 
     {
        // Draw something on the page
     }
}

// in somewhere else
foreach (Section sect in Sections)
{
     sect.Draw(something);
     Video.AddFrame(sect.Page);
}

Versus.

class Section 
{
     public Bitmap Page {/* set and get the page ...*/}
     Bitmap page; // it isn't instantiated in the class
     Draw(SomeThing s) 
     {
        // Clear the page
        // Draw something on it
     }
}

// in somewhere else
Bitmap screen = new Bitmap(600,800); // the common resource
foreach (Section sect in Sections)
{
     sect.Page = screen;
     sect.Draw(something); // it clears it before drawing something on it
     Video.AddFrame(screen);
}

Versus.

class Section 
{
     public Bitmap Page {/* set and get the page ...*/}
     static Bitmap page = new Bitmap(600,800);
     Draw(SomeThing s) 
     {
        // First Clear the page
        // Draw something on the page
     }
}

// and in somewhere else
foreach (Section sect in Sections)
{
     sect.Draw(something);
     Video.AddFrame(sect.Page);
}

First I used the first approach but it consumed a lot of memory as each object in my Sections array had a bitmap. Then I was forced to try the second approach. Is this new approach better than the former on all aspects? What is the guideline for such situations?

  • 6
    If it's static, it better be immutable. – CodesInChaos Dec 4 '14 at 11:44
  • @CodesInChaos I don't know its static or not but it consumes memory when I have many objects – Ahmad Dec 4 '14 at 11:49
  • 1
    If it is immutable, better use the flyweight pattern (a provider for the "real objects", and each real object is wrapped by a lighter one which is the one you manipulte). – SJuan76 Dec 4 '14 at 11:49
  • 3
    @Ahmad do the Bitmap contents change after creation? Can it change? This is very important to know in order to provide the correct answer. – user22815 Dec 17 '14 at 16:18
  • @Snowman Yes, I think it can be observed from the example, bitmap used to draw something on it, and each object may draw different things on it. – Ahmad Dec 17 '14 at 18:00
1

That really depends on threading model your application uses.

  • In a single-threaded sequential scenario I would either pass bitmap around (if it's construction is a costly operation) or have it created / destroyed each time. But you have to create such memory-intensive objects immediately before they are going to be used and destroy them as soon as you no longer need them if possible. Don't create them in the constructor and leave hanging around.
  • If we are about to enter multithreading you can either create a thread-safe pool of them (if construction of a bitmap is costly in your scenario) or just create /destroy each time you need it. But again, don't create objects just for fun. Create them when you need them.
2

If there is no individual bitmap for each section, it is probably not worthwhile to even have a member in Section pointing to the bitmap. You could instead pass the bitmap as a parameter to the Draw method:

class Section 
{
     Draw(Bitmap page, SomeThing s) 
     {
        // Draw something on the page
     }
}


// somewhere else

Bitmap screen = new Bitmap(600,800); // the common resource

foreach (Section sect in Sections)
{
     sect.Draw(screen, something);
     Video.AddFrame(screen);
}

That way it is more flexible and you could decide to create new resource whereever you are using the section method(s).

0

You may want to use a BitmapLoader which lazily loads the Bitmaps as and when required. This way you don't have to maintain any internal members. If required you can implement some sort of LRU caching in the BitmapLoader to optimise the loading of the most frequently accesses Bitmaps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.