I have a class with template methods for which I instantiate many other classes, like more than a hundred. The problem is that the compilation of the template class consumes a huge amount of memory, like 3GB. I assume this occurs because of many template instances. Just for detailing, the instantiated classes are Qt and QxOrm objects.
Has someone else had this problem too?
Some suggestions about how could I reduce the memory consumption?
Here are some parts of the code:
//The class with template methods
class SGSRequestHandler : public HttpRequestHandler
{
public:
SGSRequestHandler(QObject* parent = 0);
virtual ~SGSRequestHandler();
template<class T>
ResponseInfo processDatabase(ODataRequestInfo<T>& odata, qx::QxSession& session) {...}
template<class T>
ResponseInfo httpGet(ODataRequestInfo<T> &odata, qx::QxSession& session) {...}
...
}
Here is a example of what I do with the template class:
else if (className == "class1")
rInfo = process<Class1>(request, session);
else if (className == "class2")
rInfo = process<Class2>(request, session);
...
else if (className == "class100")
rInfo = process<Class100>(request, session);
I've came with a solution. Not even close from elegant, but resolved my problem.
I simply split the class that instantiate all the template in three other classes. So, the memory consumption on compilation is a third part for each of the classes.
Remembering that I need to work with templates because I'm using QxOrm library, otherelse I could do this in a much simplier manner with polimorfism.