Discussion:
Question about memory
(too old to reply)
bar
2008-03-29 19:39:53 UTC
Permalink
Hello all

My program uses
CPU 1% and Memory 10-15 MB intially.
Program still running, after 10-15 hours the CPU usage by the program is
98%. Is there any tool to detect the memory leak, i am using BCB6.

Still i didn't mention about what the program is doing, but i have couple of
questions
1.Why the memory leak or CPU usage increases.
2.If have some loop like

sample code:
for(int a=IndexOfNewsItem; a<TotalMenuItems; a++)
{
int IndexOfSubItem = NMainitem->ChildNodes->IndexOf("subitem");
if(IndexOfSubItem != -1) //No subitem found
{
int TotalMainItems = NMainitem->ChildNodes->Count;
int TotalSubItems = TotalMainItems - IndexOfSubItem;
NoOfSubItems = NoOfSubItems + TotalSubItems;
}
}
I have 4 interger variables. This loops fires may times when the program
runs.
Does it make any difference, if i move these variables in a class so that
they are declared once.

3.When multiple declarations like here are happening, what will happen to
the previous variables memory.
Any ideas greatly appreciated.
Thanks
SA
Thomas Maeder [TeamB]
2008-03-30 15:52:37 UTC
Permalink
My program uses CPU 1% and Memory 10-15 MB intially. Program still
running, after 10-15 hours the CPU usage by the program is 98%. Is
there any tool to detect the memory leak, i am using BCB6.
Why do you think that there is a memory leak?
Still i didn't mention about what the program is doing, but i have
couple of questions
1.Why the memory leak or CPU usage increases.
Swapping maybe?
2.If have some loop like
for(int a=IndexOfNewsItem; a<TotalMenuItems; a++)
{
int IndexOfSubItem = NMainitem->ChildNodes->IndexOf("subitem");
if(IndexOfSubItem != -1) //No subitem found
{
int TotalMainItems = NMainitem->ChildNodes->Count;
int TotalSubItems = TotalMainItems - IndexOfSubItem;
NoOfSubItems = NoOfSubItems + TotalSubItems;
}
}
This loop doesn't look as if it makes sense. What you have inside the
loop does not depend on the value of a and seeems not to have side
effects. Why not perform these operations just once?
I have 4 interger variables. This loops fires may times when the
program runs.
Does it make any difference, if i move these variables in a class so
that they are declared once.
Probably not, but they are already declared once now - i.e. I don't
understand your question.
3. When multiple declarations like here are happening, what will
happen to the previous variables memory.
Please elaborate what "multiple declarations" you are refering to.
bar
2008-03-30 16:35:00 UTC
Permalink
Post by Thomas Maeder [TeamB]
Please elaborate what "multiple declarations" you are refering to.
Sry for the untested code.
May be a dump question.
If a variable is declared in a loop does it causes multiple declarations
(what is the memory used in this case), like.
for(int i=0;i<10;i++)
int number;

Thanks
SA
Heath Raftery
2008-03-30 22:29:09 UTC
Permalink
Post by bar
Post by Thomas Maeder [TeamB]
Please elaborate what "multiple declarations" you are refering to.
Sry for the untested code.
May be a dump question.
If a variable is declared in a loop does it causes multiple declarations
(what is the memory used in this case), like.
for(int i=0;i<10;i++)
int number;
number is a local variable. It is local to the for loop, or in other
words its scope is the for loop. This means than any assignments to
it may be forgotten after each iteration of the loop. You can think
of this as if the int was created each time the loop is entered, and
destroyed each time the loop ends. In practice the compiler will
probably choose to create (specifically, allocate space on the stack)
the variable just once (at the start of the function), but the effect
is much the same - only one number exists at any one time, and will
not be accessible outside the for loop.

Check your favourite C or C++ reference for "variable scope" for all
the details.
--
*--------------------------------------------------------*
| ^Nothing is foolproof to a sufficiently talented fool^ |
| Heath Raftery, HRSoftWorks _\|/_ |
*______________________________________m_('.')_m_________*
Thomas Maeder [TeamB]
2008-03-31 15:29:24 UTC
Permalink
Post by bar
Post by Thomas Maeder [TeamB]
Please elaborate what "multiple declarations" you are refering to.
If a variable is declared in a loop does it causes multiple
declarations (what is the memory used in this case), like.
for(int i=0;i<10;i++)
int number;
I think I see now.

Declarations are compile-time entities. In the executable program,
a variable declaration that is also a definition materializes as
- allocation of memory to hold the variable
- possible initialization
- possible cleanup

Strictly speaking, your question therefore doesn't make sense.


How exactly a particular C++ implementation allocates memory for a
variable is an implementation detail, provided that the rules of the
language are followed.

In particular, it is left to the C++ implementation whether the
definition of the variable number causes machine code to be executed
in each run of the loop. I'd expect any decent implementation not to
generate such machine code except for the initialization, but the
language doesn't give a guarantee.
Remy Lebeau (TeamB)
2008-03-31 17:04:33 UTC
Permalink
Post by bar
My program uses
CPU 1% and Memory 10-15 MB intially.
Program still running, after 10-15 hours the CPU usage
by the program is 98%. Is there any tool to detect the
memory leak, i am using BCB6.
When CPU usage rises so high, it is usually an indication of a runaway
thread. That has nothing to do with memory leaks.
Post by bar
1.Why the memory leak or CPU usage increases.
Because you have a thread running that is not yielding control back to the
CPU in a timely manner for other threads/apps to use.
Post by bar
I have 4 interger variables. This loops fires may times when
the program runs. Does it make any difference, if i move these
variables in a class so that they are declared once.
No.
Post by bar
3.When multiple declarations like here are happening, what
will happen to the previous variables memory.
Nothing. It will not be touched at all. You should avoid duplicate
variable names to begin with, though. It is bad programming practice, and
can lead to bugs in your code if you don't pay attention to what they are
doing.


Gambit
bar
2008-04-01 08:44:41 UTC
Permalink
Hi all
Thanks for the explanation.
:)

Continue reading on narkive:
Loading...