Opened 6 years ago

Last modified 5 years ago

#1323 assigned defect

The ItemImage property leads to high memory consumption

Reported by: abeham Owned by: abeham
Priority: medium Milestone: HeuristicLab 3.3.x Backlog
Component: Core Version: 3.3.2
Keywords: Cc:

Description

The image obtained from the VS2008ImageLibrary resource manager is created anew every time the property is called. This leads on the one hand to somewhat higher memory consumption (even though this is just a small bitmap it seems to have quite an impact), but on the other hand also to leaked memory as this bitmap is not disposed anywhere.

The discussed solution so far is to remove the virtual declaration from the ItemImage property and introduce in Item a protected static Bitmap itemImage that can be set in the static initializer of a derived class defining an own icon. The property just returns this variable.

Change History (5)

comment:1 Changed 6 years ago by gkronber

I dislike the idea of static initializers for this. Can't we just have a property in Item that caches the bitmap, calling a protected virtual method CreateImage that can be overridden in sub-classes. On all subsequent calls the cached image instance is returned.

Item:
  private [static] Bitmap itemImage;
  public Bitmap ItemImage {
    [lock(locker)]
    if (itemImage==null) {
       itemImage = CreateItemImage();
    }
    return itemImage;
  }

  protected virtal Bitmap CreateItemImage() {
    object obj = ResourceManager.GetObject("Class", resourceCulture);
    return ((System.Drawing.Bitmap)(obj));
  }

If we want to have one Bitmap per type instead of one Bitmap per Item instance we can still make the itemImage field static, however, we probably also need to make the ItemImage getter thread safe.

Last edited 6 years ago by gkronber (previous) (diff)

comment:2 Changed 6 years ago by swagner

  • Owner changed from swagner to abeham
  • Status changed from new to assigned

comment:3 Changed 6 years ago by swagner

  • Milestone changed from HeuristicLab 3.3.3 to HeuristicLab x.x.x

comment:4 Changed 6 years ago by abeham

  • Priority changed from high to medium

comment:5 Changed 5 years ago by gkronber

This ticket is not resolved even after #1651 which introduced static image properties, as the Bitmap is still created via the ResourceManager each time the static getter is called.

Note: See TracTickets for help on using tickets.