simple memory profiling on Windows using the CRT debug heap

refs #81
This commit is contained in:
Mathew Sutcliffe
2013-12-15 22:24:45 +00:00
parent 388d297a2d
commit c37e160a42
2 changed files with 83 additions and 1 deletions

View File

@@ -62,7 +62,6 @@ void BlackMisc::Math::registerMetadata()
CVector3D::registerMetadata();
}
/*
* Metadata for Geo
*/
@@ -327,6 +326,75 @@ QVariant BlackMisc::complexQtTypeFromDbusArgument(const QDBusArgument &argument,
return QVariant(); // suppress compiler warning
}
#ifdef Q_CC_MSVC
#include <crtdbg.h>
// surpress some GCC warnings, if someone finds
// a better solution for this, feel free
#if defined(__GCC__) || defined(__MINGW32__) || defined(__MINGW64__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-value"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
/*
* Heap size of an object
*/
size_t BlackMisc::heapSizeOf(const QMetaType &metaType)
{
metaType.destroy(metaType.create()); // ignore one-off allocations of a class being instantiated for the first time
_CrtMemState oldState, newState, diff;
oldState.lTotalCount = newState.lTotalCount = diff.lTotalCount = 0; // avoid compiler warning
diff.lSizes[_NORMAL_BLOCK] = 0;
_CrtMemCheckpoint(&oldState);
void *p = metaType.create();
_CrtMemCheckpoint(&newState);
metaType.destroy(p);
_CrtMemDifference(&diff, &oldState, &newState);
return diff.lSizes[_NORMAL_BLOCK];
}
/*
* Heap size of an object
*/
size_t BlackMisc::heapSizeOf(const QMetaObject &metaObject)
{
delete metaObject.newInstance(); //ignore one-off allocations of a class being instantiated for the first time
_CrtMemState oldState, newState, diff;
oldState.lTotalCount = newState.lTotalCount = diff.lTotalCount = 0; // avoid compiler warning
diff.lSizes[_NORMAL_BLOCK] = 0;
_CrtMemCheckpoint(&oldState);
QObject *obj = metaObject.newInstance();
_CrtMemCheckpoint(&newState);
delete obj;
_CrtMemDifference(&diff, &oldState, &newState);
return diff.lSizes[_NORMAL_BLOCK];
}
#if defined(__GCC__) || defined(__MINGW32__) || defined(__MINGW64__)
#pragma GCC diagnostic pop
#endif
#else //!Q_OS_WIN32
/*
* Heap size of an object
*/
size_t BlackMisc::heapSizeOf(const QMetaType &)
{
qDebug() << "heapSizeOf not supported on this OS";
return 0;
}
/*
* Heap size of an object
*/
size_t BlackMisc::heapSizeOf(const QMetaObject &)
{
qDebug() << "heapSizeOf not supported on this OS";
return 0;
}
#endif //!Q_OS_WIN32
/*
* Dump all user types
*/

View File

@@ -169,6 +169,20 @@ namespace BlackMisc
*/
uint calculateHash(const QList<uint> &values, const char *className);
/*!
* \brief Real heap size of an object
* \param type
* \return
*/
size_t heapSizeOf(const QMetaType &type);
/*!
* \brief Real heap size of an object
* \param objectType
* \return
*/
size_t heapSizeOf(const QMetaObject &objectType);
} // BlackMisc
#endif // guard