- Joined
- Jun 9, 2016
- Messages
- 51
- Reaction score
- 72
Hello everyone!
I recently noticed that the allocated memory in the DatabaseServer is much higher than the actually used memory (if you have an expanded inventory you’ll see a huge difference).
Here’s a screenshot from VS Diagnostics (it shows the total memory allocated by the application):
I propose this fix to reduce the allocated memory:
This code is 100% compatible with C++14 and later.
Query.h
And:
Query.cpp
StdAfx.h (AccountServer, Certifier and DatabaseServer)
VersionCommon.h (AccountServer, Certifier and DatabaseServer) or to a common file
P.S. This code has been tested for about two months on two local servers and one live (beta) server.
I would appreciate it if you could let me know of any errors or shortcomings in the code.
I recently noticed that the allocated memory in the DatabaseServer is much higher than the actually used memory (if you have an expanded inventory you’ll see a huge difference).
Here’s a screenshot from VS Diagnostics (it shows the total memory allocated by the application):
- 310 MB RAM (without expanded slots) → 105 MB
- 1.4 GB RAM (with 168 slots / ~51,000 characters) → 105 MB
I propose this fix to reduce the allocated memory:
This code is 100% compatible with C++14 and later.
Query.h
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
#ifdef __INVENTORY_168_SLOTS
static const int MAXSYM = 51000;
#else
static const int MAXSYM = 8192;
#endif
std::vector<std::array<char, MAXSYM>> Col;
#else
char* Col[MAXCOL];
#endif
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
std::vector<std::array<SQLCHAR, 50>> ColName;
std::vector<SQLINTEGER> lCol;
#else
SQLCHAR ColName[MAXCOL][50];
SQLINTEGER lCol[MAXCOL];
#endif
In CQuery::CQuery()
Replace:
To:
In CQuery::~CQuery()
Replace:
To:
In CQuery::FindCol(char *name)
Replace:
To:
In CQuery::GetInt(int nCol)
Replace:
To:
In __int64 CQuery::GetInt64(int nCol)
Replace:
To:
In float CQuery::GetFloat(int nCol)
Replace:
To:
In char CQuery::GetChar(int nCol)
Replace:
To:
In void CQuery::GetStr(int nCol, char *buf)
Replace:
To:
In BOOL CQuery:
repareFetch()
After:
Add:
Replace:
To:
Replace:
C++:
#ifdef __INVENTORY_168_SLOTS
for (int i = 0; i < MAXCOL; i++)
Col[i] = new char[51000];
#else
for (int i = 0; i < MAXCOL; i++)
Col[i] = new char[8192];
#endif
C++:
#ifndef __QS_FIX_ALLOCATED_MEM
#ifdef __INVENTORY_168_SLOTS
for (int i = 0; i < MAXCOL; i++)
Col[i] = new char[51000];
#else
for (int i = 0; i < MAXCOL; i++)
Col[i] = new char[8192];
#endif
#endif
Replace:
C++:
for(int i = 0; i < MAXCOL; i++)
SAFE_DELETE_ARRAY( Col[i] );
C++:
#ifndef __QS_FIX_ALLOCATED_MEM
for(int i = 0; i < MAXCOL; i++)
SAFE_DELETE_ARRAY( Col[i] );
#endif
Replace:
C++:
if (stricmp(name,(LPCTSTR)ColName[i])==0)
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
if (stricmp(name, (LPCTSTR)ColName.at(i).data()) == 0)
#else
if (stricmp(name,(LPCTSTR)ColName[i])==0)
#endif
Replace:
C++:
return atoi(Col[nCol-1]);
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
return atoi(Col.at(nCol - 1).data());
#else
return atoi(Col[nCol-1]);
#endif
Replace:
C++:
return _atoi64(Col[nCol-1]);
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
return _atoi64(Col.at(nCol - 1).data());
#else
return _atoi64(Col[nCol-1]);
#endif
Replace:
C++:
return ( (float)( atof(Col[nCol-1]) ) );
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
return((float)(atof(Col.at(nCol - 1).data())));
#else
return ( (float)( atof(Col[nCol-1]) ) );
#endif
Replace:
C++:
return Col[nCol-1][0];
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
return Col.at(nCol - 1).data()[0];
#else
return Col[nCol-1][0];
#endif
Replace:
C++:
strcpy(buf,Col[nCol-1]);
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
strcpy(buf, Col.at(nCol - 1).data());
#else
strcpy(buf,Col[nCol-1]);
#endif
After:
C++:
SWORD m_nNullability;
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
if(nCol > static_cast<SQLSMALLINT>(Col.size()))
Col.resize(nCol);
if(nCol > static_cast<SQLSMALLINT>(ColName.size()))
ColName.resize(nCol);
if(nCol > static_cast<SQLSMALLINT>(lCol.size()))
lCol.resize(nCol);
#endif
C++:
#ifdef __INVENTORY_168_SLOTS
SQLBindCol(hStmt, c + 1, SQL_C_CHAR, Col[c], 51000, &lCol[c]);
#else
SQLBindCol(hStmt, c + 1, SQL_C_CHAR, Col[c], 8192, &lCol[c]);
#endif
SQLDescribeCol(hStmt,c+1,ColName[c],30,
C++:
#ifdef __QS_FIX_ALLOCATED_MEM
SQLBindCol(hStmt, c + 1, SQL_C_CHAR, Col.at(c).data(), MAXSYM, &lCol[c]);
SQLDescribeCol(hStmt,c+1,ColName.at(c).data(), 30,
#else
#ifdef __INVENTORY_168_SLOTS
SQLBindCol(hStmt, c + 1, SQL_C_CHAR, Col[c], 51000, &lCol[c]);
#else
SQLBindCol(hStmt, c + 1, SQL_C_CHAR, Col[c], 8192, &lCol[c]);
#endif
SQLDescribeCol(hStmt,c+1,ColName[c],30,
#endif
After:
Add:
C++:
#include <vector>
C++:
#include <array>
Add:
C++:
#define __QS_FIX_ALLOCATED_MEM
P.S. This code has been tested for about two months on two local servers and one live (beta) server.
I would appreciate it if you could let me know of any errors or shortcomings in the code.
Last edited:

