Welcome to MSDN Blogs Sign in | Join | Help

Memory Handles

I was asked a question about how VFP uses memory:

 

Recently, on a project to determine and resolve memory leaks in a VFP7 app (which is calling Lotus Notes v4.6), I was using the SYS(1011) function call. One thing I noticed was that the first time I called it, I would get a number (of memory handles). If I then called it again, a slightly different number would appear. If I called it a third time, it would match the second call. Is the second (and third) call the true number of memory handles?

Example:
At the command window:
SET SAFETY OFF
CREATE CURSOR w_tmp (fld1 C(50), fld2 B(6))
APPEND BLANK
APPEND BLANK
APPEND BLANK
? SYS(1011) <-- Returns 1116
? SYS(1011) <-- Returns 1115
? SYS(1011) <-- Returns 1115

ZAP
? SYS(1011) <-- Returns 1228
? SYS(1011) <-- Returns 1224
? SYS(1011) <-- Returns 1224

 

Sounds like the Heisenberg Uncertainty Principle at work.

 

VFP can appear to be idle, perhaps at the command prompt, and yet be doing things in the background. That’s the same with any other application: appearances can be deceiving.

 

For example, if you move your mouse or type a key when VFP is active, there are many Windows messages that are sent to VFP. You can use SPY to see these messages. VFP also does things in the background that can allocate/free memory.

 

So what does “true number of memory handles” mean? It’s a number that fluctuates quite a bit, even if it appears that VFP is idle.

 

Doesn’t that make SYS(1011) useless ? How can you use it to determine if there’s a memory leak?

 

Put your test in a loop and see if the memory handles grow unexpectedly. (Obviously, if your loop creates objects without releasing them, then at the end of the loop, you’ll have lots of objects and used memory handles.)

 

What is a memory handle? A memory handle is just a number that represents some allocated memory of a particular size.

 

Suppose VFP needs to allocate 1000 bytes of memory. It calls a routine that allocates a block of memory and returns a number (called a handle) representing that memory. That memory can be freed by passing that handle to a free routine. Some memory allocations will be needed longer than others, while others might need to be resized as needs change. Memory fragmentation and compaction can occur. The memory could be moved around physically to a different address. .When that memory is used, the handle is passed to a routine that returns the actual physical address of the memory.

 

The Library Construction Kit memory management functions allow you to allocate/free memory directly in Foxpro. The GlobalAlloc function of the Windows API also returns handles.

 

 

 

 51512

Published Thursday, February 17, 2005 9:32 AM by Calvin_Hsia
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Memory Handles

Thursday, July 28, 2005 3:54 AM by Sanjay Patel
Interesting finding.I dont have VFP7 to test the above code but I tested the code in VFP 9. VFP9 does not have the mem leak problem and all the values returned were constant throughout the code execution.

# thanks

Wednesday, November 02, 2005 9:30 AM by Global
Thank you for the information!

# explicate Hamburg

Tuesday, January 16, 2007 10:48 AM by explicate Hamburg

thoughts from a professional developer

I do not agree. Go to http://www.besthotelsite.info/motivation_Germany/plunging_Hamburg/explicate_Hamburg_1.html

# elude Hamburg

Tuesday, January 16, 2007 12:08 PM by elude Hamburg

thoughts from a professional developer

I do not agree. Go to http://www.myhotelhome.info/equivalence_Germany/favour_Hamburg/elude_Hamburg_1.html

# octaroon Vienna

Wednesday, January 24, 2007 4:06 AM by octaroon Vienna

thoughts from a professional developer

I do not agree. Go to http://www.businessworkz.info/weather_Austria/ess_Eastern%20Austria/octaroon_Vienna_1.html

# re: Memory Handles

Tuesday, March 13, 2007 1:06 PM by ...

E evidente che il luogo e stato fatto dalla persona che realmente conosce il mestiere!

# re: Memory Handles

Thursday, March 15, 2007 10:53 PM by ...

luogo interessante, soddisfare interessante, buon!

# re: Memory Handles

Saturday, March 17, 2007 3:45 PM by ...

Lo trovo piuttosto impressionante. Lavoro grande fatto..)

# re: Memory Handles

Monday, March 19, 2007 6:59 AM by ...

Stupore! ho una sensibilit molto buona circa il vostro luogo!!!!

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker