Memory Leaks?

  • Thread starter Thread starter Tony Penaherrera
  • Start date Start date
T

Tony Penaherrera

What is memory leak? Does it ever leak? What cause memory to leak? Someone
had asked me yesterday and I wasn't able to give him a clear answer.

Any comment would be greatly appreciated

Tony
 
Memory leak generally means an application that when closed does
not relinquish all memory back to the available pool for XP to use.
Over time, this can cause performance problems or XP to malfunction.
Leaks are a "Serious" application defect, that on modern programs
should not occur.
 
R. McCarty said:
Memory leak generally means an application that when closed does
not relinquish all memory back to the available pool for XP to use.
Over time, this can cause performance problems or XP to malfunction.
Leaks are a "Serious" application defect, that on modern programs
should not occur.

To flesh this out a bit, memory leaks are a far greater problem in scripts
(WSH, VBS, JS and third-party) than in self-contained EXEs or DLLs, that
have control over their own memory access. There, as pointed out in the
prior post, the issue is simply one of decent programming.

The current generation of scripts operate through opening COM object
interfaces that provide access to the file system, shell, registry and
applications. Many applications, such as IE and the MS Office applications,
as well as many third-party apps, are all designed to function as COM
objects for scripting, either through associated DLLs or the EXE file
itself, as well as functioning as applications.

When a typical COM class object is "accessed" by the creation of an
"instance" of the object, a pointer to the IDispatch interface and a new
virtual "object" is created as an interface definition of parameters with
assigned memory. Memory is allocated in which the new object's members may
store data. For the typical COM class object, a new pointer and virtual
object are created for each instance of the underlying object accessed.

If the COM class object is a "singleton" object, however, only a single
instance will be created, which will be shared not only by all object
variable instances in the same script, but by all scripts or other processes
accessing the object. In the case of a singleton object, the initiation
process first checks to see if an instance of the underlying object has
already been instantiated and, if so, returns a pointer to the same initial
virtual object.

When a script terminates, WSH runs a cleanup process that deletes the
pointers and begins the process of eliminating the interface and memory
allocation. An object will be disconnected only after all references to it
have been released, and Windows may retain certain references, depending on
the object type and how it was instantiated. It is possible, for example,
to lock an object, when employing multiple object references. Care must be
taken in writing a script with techniques and object types that allow the
object instance to continue beyond its scope in the script or even beyond
the termination of the script.

A review of posts on the VBS and WSH newsgroups relating to concerns with
continued object connections and "memory leaks" seems to indicate a number
of reasonably distinct situations, including, among others: (1) failure to
properly exit a With statement block by progressing through the End With
statement (a With statement provides a semi-permanent connection to a COM
object and must be formally exited); (2) multiple or duplicate object
references, where object variables in higher levels of scope are not
released; and (3) recursive procedures that create multiple copies of an
object.

The most pernicious scripting "memory leaks" appear to involve EXE or
special DLL objects, that are essentially applications, and which, once
initiated, continue independently, despite being released from the object
connections to the script. With these types of objects, a well-written
script should force the object to shut down through internal means, usually
an object's Quit method, before terminating the script's object reference.

As an example, it is possible for a badly written script accessing the
InternetExplorer.Application as a COM object, to leave open an "invisible"
instance of IE that is no longer needed, but is operating and potentially
progressively chewing up memory. I've used an example of an app-COM object
for clarity, but the same problem could occur if a script has locked open a
COM-only object. And the problem is not limited to badly written scripts,
but also to badly written and compiled thrird-party COM objects that may be
installed voluntarily or by app installations, which can refuse to close
properly or which have the same type of "memory leaks" as a badly written
EXE (i.e., poor memory management and release). On the bright side, any COM
objects that a script leaves open can be found in Task Manager as an active
process and manually closed.

You might want to do a Google Groups search for "memory leaks" on the
scripting.vbs or scripting.wsh newsgroups for some nicely detailed
discussions and examples of the problem in regard to scripts.

Joe Earnest
 
Tony Penaherrera said:
What is memory leak? Does it ever leak? What cause memory to leak? Someone
had asked me yesterday and I wasn't able to give him a clear answer.

It's a programming mistake. Programs constantly obtain memory from the
operating system. Generally, when they finish with it they are
supposed to return it. If a program asks for memory but doesn't return
it when it finishes with it, the memory has "leaked". If this keeps
up, eventually the program will ask for memory and be told that there
isn't any.
 
It's a programming mistake. Programs constantly obtain memory from the
operating system. Generally, when they finish with it they are
supposed to return it. If a program asks for memory but doesn't return
it when it finishes with it, the memory has "leaked". If this keeps
up, eventually the program will ask for memory and be told that there
isn't any.

Just to be clear, what is supposed to happen is that when an application is
closed, the application code may still be in the system cache (assuming
sufficient RAM, of course), but it should no longer be taking up the memory
used for applications, right?
 
Ken Gardner said:
Just to be clear, what is supposed to happen is that when an application is
closed, the application code may still be in the system cache (assuming
sufficient RAM, of course), but it should no longer be taking up the memory
used for applications, right?

Each application has a 4GB virtual memory space all its own. When the
program ends, the operating system gets rid of this virtual memory
space. At that point, any memory that leaked is no longer a factor.
 
Tim Slattery said:
Each application has a 4GB virtual memory space all its own. When the
program ends, the operating system gets rid of this virtual memory
space. At that point, any memory that leaked is no longer a factor.

This isn't making sense to me. There is no way that my pagefile is 4GB for
each application. :) But isn't your broader point here that the application
is supposed to release its allocated portion of virtual memory when it is
closed? If so, how is this a significant problem on any machine with lots of
RAM to begin with (e.g. more than 512 MB)? In other words, why are memory
leaks even a problem if they don't soak up RAM for either applications or the
system cache on systems with lots of RAM? TIA.

Ken
 
Ken Gardner said:
This isn't making sense to me. There is no way that my pagefile is 4GB for
each application. :)

I didn't say that it was. I said that each application has a 4GB
virtual memory space available to it. Each app thinks that it is in a
4GB space. If each app actually tried to allocate 4GB of memory, there
would be a problem.
But isn't your broader point here that the application
is supposed to release its allocated portion of virtual memory when it is
closed?

No, while it's running. If you have, for example, a server program
that runs 24 hours a day and leaks 1 MB of memory each hour, in a few
days it's going to start having problems. If you have an application
program that does this, but that is started, used for twenty minutes,
then closed, you may never notice it.

But if this 1MB leak is in a loop that gets executed 10,000 times
every hour, then you've got a problem.

But in the 32-bit Windows XP world (and Unix, for that matter), when a
process ends the OS releases all memory that it was using.

In 16-bit Windows and the Windows 9x systems this was not true,
especially regarding the "system resources". Those were two 64-KB
heaps that programs were supposed to use as needed and return when
finished. In that world, if a program did not return "system
resources", that space was gone until the OS was rebooted.
 
perris said:
the operating system keeps as much data in memory as it can, while
making as much memory available as it can in the same stroke...this is
EXCELLANT memory management policy

Yes...much better on XP than earlier versions of Windows. And much more
stable, too. It is really fun to leave your computer running for weeks
without rebooting, and using it for all sorts of different applications --
and still seeing that at the end of the day I have a ton of RAM left over for
applications or the system cache and the thing is running like a gazelle.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Memory Leak 2
Error Message on Closing 1
how to interpret poolmon output, 'Proc' tag. 21
Memory leak during backup 17
how do I detect a memory leak? 3
Memory Leak 2
KB885250 1
ID memory leak 5

Back
Top