Ananth wrote:
> Andreanes Yosef Vanderlee <[EMAIL PROTECTED]> wrote:
>> Hi... I have a problem with this code:
>>  ----------------------------------------------------------
>>  do{
>>      delay(1500); /* if I remove this, memory usage will grown so fast */
>>      close(tcp_socket);
>>      tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
>>      ret2 = connect(tcp_socket, (struct sockaddr*)&peer, sizeof(peer));
>>  }while(ret2 != 0);
>>  ----------------------------------------------------------
>>
>>  that part of code will try to connect to server as long as the server
>>  is still down.
>>
>>  My problem is, if I remove delay there, memory usage will grown very
>>  fast, so does the CPU usage (in 1 tested pc, it hang).
>>
>>  But if I put that delay, the CPU usage is not high, but the memory
>>  usage is still going bigger n bigger as long as the server is down.
>>
>>  I try to put that delay, and cut the connection to the server (so that
>>  it'll always run that part of my code) for about 1 hour, and the
>>  memory usage is growing from 1.8MB to 6.8MB.
>>
>>  Should I put something on my code to refresh the memory?
> 
> I had this problem in one of my projects in the past, CPU shooting to 100%.
> I put in a Sleep(100) and that solved the problem.
> Don't know if there's a better way, but the Sleep solved my issues.
> 
> Cheers
> - Ananth

Ananth:  Read the problem carefully.  The OP is experiencing a memory 
allocation/growth issue NOT a performance issue.

OP:  I've looked at that code every-which-way and honestly don't see 
_anything_ wrong with it.  It appears to follow the manpage description 
to the letter.  However, there are four things you should try for the 
just-in-case-beat-head-into-brick-wall-for-doing-something-really-stupid 
scenario:

1)  Check for errors from socket() and close().
2)  When an error occurs from connect(), display the errno value.  Just 
for sanity's sake.
3)  Move the close() statement so that it is before the delay() 
statement.  Also, make sure it is actually a socket before using the call.
4)  Try zeroing and rebuilding the sockaddr structure (peer).

If all that fails to produce something, it could just be the way Linux 
operates - or a bug in the kernel.  (You would think someone would 
notice a bug like that, though).

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to