Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory leak

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
US
Hi,

I am wondering if there is anyway I could keep track of how much memory is being allocated in my program. I have been given a huge program of 5000 lines of code and asked to fix memory leak. It contains some pretty huge data structures. It is majorly for database connection, open, fetch, close. THe problem I am facing is when I use the statement "select * from [databasename] where [key] is between 1000 and 99000". In the begining it used to do a core dump then i found that it is running out of memory, i checked through the code and added some free statements to release allocated memory, I found that it just bails out displaying some 20,000 records without any warning. What I think the prob might be is even thought the memory is available it is unable to use the freed memory. I am working real hard to fix this bug any help would be greatly appreciated.
 
If all malloc/free is made in your code you can write a wraper for malloc and free command and replaced it by define:
#define malloc my_malloc
#define free my_free
and count all allocated and freed memory.
It will not help you if memory allocated outside your program.
Also if you are on UNIX use "top" command to see total memory usage by your programm, but keep in mind that some unixes do not return memory back to system when it freed, but they can use it for themself.
More easy way to use "purify" if you have it. In this case you don't have to do much, just build program with purify (or purify existing executable or shared library) and it will show your all memory leaks.
 
if you are using windows programming then there is a option of knowning how much memory you are useing.

assuming its any platform
In case of database connections when you are using odbc
see the following.
1)is the column you are retrieving from
has a size which is more than the size you allocate.
2) see the connection handles allocation and its subsequent
freeing operation.

now regarding memory, do one thing see all the mallocs and
the corresponding free operations,weather the same memory
allocated once is freeds or each time you reuse the same

DO ONE MORE THING CLEAN THE MEMORY WHEN YOU USE DYNAMIC ALLOCATION USING memset() or anything else

hawapani



chunk.

 
May be you should try compiling your program using larger memory model (may be huge if required). Doing this would make the scope of the program variables and pointers extend to various memory segments which are otherwise limited to only on from where the code is executing...

I hope it helps...:)

Roy.
user.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top