Code consuming lot of RAM (Memory)
-
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction. "main.cpp"
#include "ESSolver.h"
using namespace std;int maxIt;
int minVal;
int maxVal;int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;if( function\_eval == 1){ minVal = -32; maxVal = 32; } ESSolver es(nVar, mu, lbd, rhoO, rhoS); es.function\_eval = function\_eval; es.Init(); es.Optimize(); return 0;
}
"ESSolver.cpp"
#include #include #include "ESSolver.h"
#include using namespace std;ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);for (int i = 0; i < mu; i++) { muPop\[i\].resize(nVar); muSigma2\[i\].resize(nVar); } lambdaPop.resize(lbd); lambdaSigma.resize(lbd); lambdaSigma2.resize(lbd); for (int i = 0; i < lbd; i++) { lambdaPop\[i\].resize(nVar); lambdaSigma2\[i\].resize(nVar); } mulambdaPop.resize(mu+lbd); mulambdaSigma.resize(mu+lbd); mulambdaSigma2.resize(mu+lbd); for (int i = 0; i < mu+lbd; i++) { mulambdaPop\[i\].resize(nVar); mulambdaSigma2\[i\].resize(nVar); } rhoPop.resize(rhoO); rhoSigma.resize(rhoS); rhoSigma2.resize(rhoS); for (int i = 0; i < rhoO; i++) { rhoPop\[i\].resize(nVar); } for (int i = 0; i < rhoS; i++) { rhoSigma2\[i\].resize(nVar); } return;
}
ESSolver::~ESSolver(void) {
return;
}void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}if (mutOp == 0) { for (int i = 0; i < mu; i++) { muSigma\[i\] = rand01\_r(); } } else { for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { muSigma2\[i\]\[j\] = rand01\_r(); } } }
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
// Manipulating values of
// muPop, muSigma, muSigma2,
// lambdaPop, lambdaSigma, lambdaSigma2,
// mulambdaPop, mulambdaSigma, mulambdaSigma2, -
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction. "main.cpp"
#include "ESSolver.h"
using namespace std;int maxIt;
int minVal;
int maxVal;int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;if( function\_eval == 1){ minVal = -32; maxVal = 32; } ESSolver es(nVar, mu, lbd, rhoO, rhoS); es.function\_eval = function\_eval; es.Init(); es.Optimize(); return 0;
}
"ESSolver.cpp"
#include #include #include "ESSolver.h"
#include using namespace std;ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);for (int i = 0; i < mu; i++) { muPop\[i\].resize(nVar); muSigma2\[i\].resize(nVar); } lambdaPop.resize(lbd); lambdaSigma.resize(lbd); lambdaSigma2.resize(lbd); for (int i = 0; i < lbd; i++) { lambdaPop\[i\].resize(nVar); lambdaSigma2\[i\].resize(nVar); } mulambdaPop.resize(mu+lbd); mulambdaSigma.resize(mu+lbd); mulambdaSigma2.resize(mu+lbd); for (int i = 0; i < mu+lbd; i++) { mulambdaPop\[i\].resize(nVar); mulambdaSigma2\[i\].resize(nVar); } rhoPop.resize(rhoO); rhoSigma.resize(rhoS); rhoSigma2.resize(rhoS); for (int i = 0; i < rhoO; i++) { rhoPop\[i\].resize(nVar); } for (int i = 0; i < rhoS; i++) { rhoSigma2\[i\].resize(nVar); } return;
}
ESSolver::~ESSolver(void) {
return;
}void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}if (mutOp == 0) { for (int i = 0; i < mu; i++) { muSigma\[i\] = rand01\_r(); } } else { for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { muSigma2\[i\]\[j\] = rand01\_r(); } } }
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
// Manipulating values of
// muPop, muSigma, muSigma2,
// lambdaPop, lambdaSigma, lambdaSigma2,
// mulambdaPop, mulambdaSigma, mulambdaSigma2,seems mostly with vector resize which you are using extensively.
-
seems mostly with vector resize which you are using extensively.
-
single run takes more than 1 MB
-
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction. "main.cpp"
#include "ESSolver.h"
using namespace std;int maxIt;
int minVal;
int maxVal;int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;if( function\_eval == 1){ minVal = -32; maxVal = 32; } ESSolver es(nVar, mu, lbd, rhoO, rhoS); es.function\_eval = function\_eval; es.Init(); es.Optimize(); return 0;
}
"ESSolver.cpp"
#include #include #include "ESSolver.h"
#include using namespace std;ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);for (int i = 0; i < mu; i++) { muPop\[i\].resize(nVar); muSigma2\[i\].resize(nVar); } lambdaPop.resize(lbd); lambdaSigma.resize(lbd); lambdaSigma2.resize(lbd); for (int i = 0; i < lbd; i++) { lambdaPop\[i\].resize(nVar); lambdaSigma2\[i\].resize(nVar); } mulambdaPop.resize(mu+lbd); mulambdaSigma.resize(mu+lbd); mulambdaSigma2.resize(mu+lbd); for (int i = 0; i < mu+lbd; i++) { mulambdaPop\[i\].resize(nVar); mulambdaSigma2\[i\].resize(nVar); } rhoPop.resize(rhoO); rhoSigma.resize(rhoS); rhoSigma2.resize(rhoS); for (int i = 0; i < rhoO; i++) { rhoPop\[i\].resize(nVar); } for (int i = 0; i < rhoS; i++) { rhoSigma2\[i\].resize(nVar); } return;
}
ESSolver::~ESSolver(void) {
return;
}void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}if (mutOp == 0) { for (int i = 0; i < mu; i++) { muSigma\[i\] = rand01\_r(); } } else { for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { muSigma2\[i\]\[j\] = rand01\_r(); } } }
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
// Manipulating values of
// muPop, muSigma, muSigma2,
// lambdaPop, lambdaSigma, lambdaSigma2,
// mulambdaPop, mulambdaSigma, mulambdaSigma2,What do you mean by lot of memory? I think that about 2M are used by the
ESSolver
instance of your example code. -
What do you mean by lot of memory? I think that about 2M are used by the
ESSolver
instance of your example code. -
single run takes more than 1 MB
-
It must be somewhere else in your code because I have compiled the example code and it uses 3355 KiB (waiting for input at end of main and checked the process on another shell in Linux).
-
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction. "main.cpp"
#include "ESSolver.h"
using namespace std;int maxIt;
int minVal;
int maxVal;int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;if( function\_eval == 1){ minVal = -32; maxVal = 32; } ESSolver es(nVar, mu, lbd, rhoO, rhoS); es.function\_eval = function\_eval; es.Init(); es.Optimize(); return 0;
}
"ESSolver.cpp"
#include #include #include "ESSolver.h"
#include using namespace std;ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);for (int i = 0; i < mu; i++) { muPop\[i\].resize(nVar); muSigma2\[i\].resize(nVar); } lambdaPop.resize(lbd); lambdaSigma.resize(lbd); lambdaSigma2.resize(lbd); for (int i = 0; i < lbd; i++) { lambdaPop\[i\].resize(nVar); lambdaSigma2\[i\].resize(nVar); } mulambdaPop.resize(mu+lbd); mulambdaSigma.resize(mu+lbd); mulambdaSigma2.resize(mu+lbd); for (int i = 0; i < mu+lbd; i++) { mulambdaPop\[i\].resize(nVar); mulambdaSigma2\[i\].resize(nVar); } rhoPop.resize(rhoO); rhoSigma.resize(rhoS); rhoSigma2.resize(rhoS); for (int i = 0; i < rhoO; i++) { rhoPop\[i\].resize(nVar); } for (int i = 0; i < rhoS; i++) { rhoSigma2\[i\].resize(nVar); } return;
}
ESSolver::~ESSolver(void) {
return;
}void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}if (mutOp == 0) { for (int i = 0; i < mu; i++) { muSigma\[i\] = rand01\_r(); } } else { for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { muSigma2\[i\]\[j\] = rand01\_r(); } } }
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
// Manipulating values of
// muPop, muSigma, muSigma2,
// lambdaPop, lambdaSigma, lambdaSigma2,
// mulambdaPop, mulambdaSigma, mulambdaSigma2,As already noted, I have compiled and run your code. The result is as expected: It consumes less than 4 MiB of memory. A common case for running out of memory is - besides allocating huge amounts of memory (e.g. many large objects) - assigning newly created instances to pointer member variables without deleting the old instance.
-
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction. "main.cpp"
#include "ESSolver.h"
using namespace std;int maxIt;
int minVal;
int maxVal;int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;if( function\_eval == 1){ minVal = -32; maxVal = 32; } ESSolver es(nVar, mu, lbd, rhoO, rhoS); es.function\_eval = function\_eval; es.Init(); es.Optimize(); return 0;
}
"ESSolver.cpp"
#include #include #include "ESSolver.h"
#include using namespace std;ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);for (int i = 0; i < mu; i++) { muPop\[i\].resize(nVar); muSigma2\[i\].resize(nVar); } lambdaPop.resize(lbd); lambdaSigma.resize(lbd); lambdaSigma2.resize(lbd); for (int i = 0; i < lbd; i++) { lambdaPop\[i\].resize(nVar); lambdaSigma2\[i\].resize(nVar); } mulambdaPop.resize(mu+lbd); mulambdaSigma.resize(mu+lbd); mulambdaSigma2.resize(mu+lbd); for (int i = 0; i < mu+lbd; i++) { mulambdaPop\[i\].resize(nVar); mulambdaSigma2\[i\].resize(nVar); } rhoPop.resize(rhoO); rhoSigma.resize(rhoS); rhoSigma2.resize(rhoS); for (int i = 0; i < rhoO; i++) { rhoPop\[i\].resize(nVar); } for (int i = 0; i < rhoS; i++) { rhoSigma2\[i\].resize(nVar); } return;
}
ESSolver::~ESSolver(void) {
return;
}void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}if (mutOp == 0) { for (int i = 0; i < mu; i++) { muSigma\[i\] = rand01\_r(); } } else { for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { muSigma2\[i\]\[j\] = rand01\_r(); } } }
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
// Manipulating values of
// muPop, muSigma, muSigma2,
// lambdaPop, lambdaSigma, lambdaSigma2,
// mulambdaPop, mulambdaSigma, mulambdaSigma2,AtineshS wrote:
On running it, I have noticed that it consumes lots of RAM (Memory).
How are you verifying this?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
AtineshS wrote:
On running it, I have noticed that it consumes lots of RAM (Memory).
How are you verifying this?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
If that's anything like Task Manager in Windows, you should not be using it as a "how much RAM is my program using" gauge. Task Manager is showing you the address space that is in use, which has little (to nothing) to do with the amount of that address space your program is actually using. The "memory" numbers are all but completely useless for telling how much of your memory is actually in use. Think of it more as a high-water mark. While you may have made an allocation at one point in time, freeing the blocks does not guarantee the address space numbers will go down. Those blocks are on the free list, still in the address space, but they are available for subsequent allocation.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction. "main.cpp"
#include "ESSolver.h"
using namespace std;int maxIt;
int minVal;
int maxVal;int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;if( function\_eval == 1){ minVal = -32; maxVal = 32; } ESSolver es(nVar, mu, lbd, rhoO, rhoS); es.function\_eval = function\_eval; es.Init(); es.Optimize(); return 0;
}
"ESSolver.cpp"
#include #include #include "ESSolver.h"
#include using namespace std;ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);for (int i = 0; i < mu; i++) { muPop\[i\].resize(nVar); muSigma2\[i\].resize(nVar); } lambdaPop.resize(lbd); lambdaSigma.resize(lbd); lambdaSigma2.resize(lbd); for (int i = 0; i < lbd; i++) { lambdaPop\[i\].resize(nVar); lambdaSigma2\[i\].resize(nVar); } mulambdaPop.resize(mu+lbd); mulambdaSigma.resize(mu+lbd); mulambdaSigma2.resize(mu+lbd); for (int i = 0; i < mu+lbd; i++) { mulambdaPop\[i\].resize(nVar); mulambdaSigma2\[i\].resize(nVar); } rhoPop.resize(rhoO); rhoSigma.resize(rhoS); rhoSigma2.resize(rhoS); for (int i = 0; i < rhoO; i++) { rhoPop\[i\].resize(nVar); } for (int i = 0; i < rhoS; i++) { rhoSigma2\[i\].resize(nVar); } return;
}
ESSolver::~ESSolver(void) {
return;
}void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}if (mutOp == 0) { for (int i = 0; i < mu; i++) { muSigma\[i\] = rand01\_r(); } } else { for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { muSigma2\[i\]\[j\] = rand01\_r(); } } }
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
// Manipulating values of
// muPop, muSigma, muSigma2,
// lambdaPop, lambdaSigma, lambdaSigma2,
// mulambdaPop, mulambdaSigma, mulambdaSigma2, -
Below is the simplified version of the code I am using in my project. On running it, I have noticed that it consumes lots of RAM (Memory). I am not getting why is that happening, can anybody guide me in the right direction. "main.cpp"
#include "ESSolver.h"
using namespace std;int maxIt;
int minVal;
int maxVal;int main(){
int function_eval = 1, nVar = 500, mu = 15, lbd = 100, rhoO = 2, rhoS = mu;
maxIt = 1000;if( function\_eval == 1){ minVal = -32; maxVal = 32; } ESSolver es(nVar, mu, lbd, rhoO, rhoS); es.function\_eval = function\_eval; es.Init(); es.Optimize(); return 0;
}
"ESSolver.cpp"
#include #include #include "ESSolver.h"
#include using namespace std;ESSolver::ESSolver(int _nVar, int _mu, int _lbd, int _rhoO, int _rhoS):nVar(_nVar), mu(_mu), lbd(_lbd), rhoO(_rhoO), rhoS(_rhoS)
{
muPop.resize(mu);
muSigma.resize(mu);
muSigma2.resize(mu);for (int i = 0; i < mu; i++) { muPop\[i\].resize(nVar); muSigma2\[i\].resize(nVar); } lambdaPop.resize(lbd); lambdaSigma.resize(lbd); lambdaSigma2.resize(lbd); for (int i = 0; i < lbd; i++) { lambdaPop\[i\].resize(nVar); lambdaSigma2\[i\].resize(nVar); } mulambdaPop.resize(mu+lbd); mulambdaSigma.resize(mu+lbd); mulambdaSigma2.resize(mu+lbd); for (int i = 0; i < mu+lbd; i++) { mulambdaPop\[i\].resize(nVar); mulambdaSigma2\[i\].resize(nVar); } rhoPop.resize(rhoO); rhoSigma.resize(rhoS); rhoSigma2.resize(rhoS); for (int i = 0; i < rhoO; i++) { rhoPop\[i\].resize(nVar); } for (int i = 0; i < rhoS; i++) { rhoSigma2\[i\].resize(nVar); } return;
}
ESSolver::~ESSolver(void) {
return;
}void ESSolver::Init() {
for (int i = 0; i < mu; i++) {
for (int j = 0; j < nVar; j++) {
muPop[i][j] = rand_r(minVal, maxVal);
}
}if (mutOp == 0) { for (int i = 0; i < mu; i++) { muSigma\[i\] = rand01\_r(); } } else { for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { muSigma2\[i\]\[j\] = rand01\_r(); } } }
}
void ESSolver::Optimize() {
int it = 0;
while (it < maxIt){
// Manipulating values of
// muPop, muSigma, muSigma2,
// lambdaPop, lambdaSigma, lambdaSigma2,
// mulambdaPop, mulambdaSigma, mulambdaSigma2,