Question: I've got a template class with an integer parameter, but I only know the template parameter at runtime. Is there any best practice to create template classes at runtime?

The solution I've come up with is to create an abstract base class of the template class, that provides the interface and have an adapter class, that creates the template class and stores it in a pointer of the type of the base class. 

```
class MyInterface {
    virtual void doSomething(...) = 0;
}

template <int T>
class MyTemplateClass : public MyInterface {
    void doSomething(...) { ... };
}

class TemplateAdapter {
    MyInterface* template_class;

    Template(int n) {
        switch(n) {
          case 1:
            template_class = new MyTemplateClass<1>();
            break;
          case 2:
            template_class = new MyTemplateClass<2>();
            break;
          case 3:
            template_class = new MyTemplateClass<3>();
            break;
          [...]
        }
    }

   void doSomething() {
       template_class->doSomething();
   }
}

```

Now while this does work and yields the correct results, it is very slow. It is almost twice as slow using the adapter than using the template class. It is clear that is has to be somewhat slower, but this is far slower than I'd expected.

Where does this big loss in performance come from? And do you know how to dynamically create a template class with a better performance?

Any help is greatly appreciated!
Thanks,
Pedro

Answer: 
try to load your Assets outside the update method, like in the constructor, maybe it loads every time the update runs which is by deltaTime or framerate or whatever and its overloading, i mean update methods usualy run like 100 times a second maybe everytime you call g.newPixmap() it allocates memory for a bitmap each time
Re use the BitmapFactory instance and do not make a new instance for each image that your using. 

After the call to decodeStream which takes an InputStream argument, you should free all resources in your concrete implementation of this class. Directly calling close on InputStream does nothing.