memory problems when creating/shutting down of socket

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

memory problems when creating/shutting down of socket

825 Views
vines
Contributor III

Hi,

Here is the scenario:

1. When I try to connect/disconnect the socket on my device a million times by sending notification to the board, everything is working fine.

2. However, for the use case mentioned below I am having a memory problem:

     1. Connect to the board

     2.Kill the application software by task manager and  not sending a close notification to the board.

     3. Re-launch the application and try to reconnect

     4. The connection is fine and I can communicate perfectly. However I saw the size of my ram to increase with this.

     5. When I repeat steps 2-4, it will come to a point that my memory is not enough and my task is giving an out of memory blocks problem. With each           passing cycle it adds 9k+ to the memory(which is the memory of socket stack below)

When I call RTCS_create it alloc a memory for the TCPIP:

500  0x10001  TCP Control Block;RTCS/TCP

4392   0x10001  TCP Tx Window;RTCS/TCP

4392   0x10001  TCP Rx Window;RTCS/TCP

148 0x10001  TCP Send Clock;RTCS/TCP


However, isn't the purpose of shutdown(handle,FLAG_CLOSE_TX) to free this memory? Correct me if I am wrong.


Here is a snippet of my code:


func()

{

//some code here

     _RTCS_socket_part_init = 4;
     _RTCS_socket_part_grow = 2;
     _RTCS_socket_part_max  = 20;


    gLocalAddr.sin_family = AF_INET;
    gLocalAddr.sin_port = getTCP_port();//TCP_PORT;
    gLocalAddr.sin_addr.s_addr = INADDR_ANY;

    gSock = socket(PF_INET, SOCK_STREAM, 0); // pag TCP, SOCK_STREAM ang gagamitin
    if (gSock == RTCS_SOCKET_ERROR)
        os_task_block();

    error = bind(gSock, &gLocalAddr, sizeof(sockaddr_in));
    if (error != RTCS_OK)
        os_task_block();
   
    if (listen(gSock, 0) != RTCS_OK)
        os_task_block();
    gPeerLen = sizeof(gPeerAddr);

    while (TRUE)
    {
        activeSock = RTCS_selectall(0);
        if (activeSock == gSock)
        {
            gChildHandle = accept(gSock, &gPeerAddr, &gPeerLen);
            if (gChildHandle != RTCS_SOCKET_ERROR)
            {
                if (error == RTCS_OK)
                {
                    if (gChildHandle != RTCS_SOCKET_ERROR)
                    {
                        os_time_delay(1000);
                        gTcpLinkUp = true;
                        while (gTcpLinkUp)
                        {
                            os_mutexlockwhileloopcheck(&Nic_mutex,5);
                            activeSock = RTCS_selectall(0);
                            if (activeSock != gChildHandle &
activeSock != 0) //this will detect if connection request done other than gChildHandle
                                gTcpLinkUp=false;
                            if (ipcfg_get_link_active(BSP_DEFAULT_ENET_DEVICE) == FALSE)
                                gTcpLinkUp = false;
                           
                            os_time_delay(100);
                            os_mutex_unlock(&Nic_mutex);
                        }
                        shutdown(gChildHandle, FLAG_CLOSE_TX);
                    }
                }
                else
                {
                    shutdown(gChildHandle, FLAG_CLOSE_TX);
                }
            }
        }
        os_time_delay(100);
    }

}


As an additional question:


There is an option to reduce the size of the socket stack by calling setsockopt. However I don't think it is working for me. The reason is because whenever the scenario discussed above happen, it recreates the whole memory below.


500      0x10001  TCP Control Block;RTCS/TCP
4392   0x10001  TCP Tx Window;RTCS/TCP
4392   0x10001  TCP Rx Window;RTCS/TCP
148     0x10001  TCP Send Clock;RTCS/TCP


I was expecting 512bytes only for both TCP Rx and Tx.Thus it should be around 1k+ only.


Here is the snippet

#define MAX_MESSAGE 512

opt_value=MAX_MESSAGE;
error = setsockopt(
gChildHandle , SOL_TCP, OPT_RBSIZE, &opt_value,2);
error = setsockopt(
gChildHandle , SOL_TCP, OPT_TBSIZE, &opt_value,2);

//status of error after this is RTCS_OK



I read somewhere to do it before RTCS_create. However, is it really possible because setsockopt function needs a handle. Before RTCS_Create it is still not created.


Thank You.

0 Kudos
3 Replies

336 Views
Monica
Senior Contributor III

Hello Vines,

Was that workaround helpful? Keep us posted :smileywink:

Best regards,

Monica

0 Kudos

336 Views
vines
Contributor III

Hi Martin, Monica,

The suggestion was helpful to an extent. I revise the program by passing FLAG_ABORT_CONNECTION on the shhutdown function. I read on the datasheet that it destroys the socket immediately without waiting for existing transmission or reception to finish. Thus am I right to assume that this should free up the memory? However what is happening now is when I try to connect again it does not free the previous one even if I shut it down. Thus it creates 2 stack for the RTCS.

The improvement however is even I try to connect/shutdown couple of times it only caters for another set of stack (so 2 sets only regardless of how many times I connect and shutdown). As compared to previously where in my stack are running out depends on how fast I connect/shutdown the socket (if within 2 minutes).

Any suggestion on how to eliminate the mirror copy of RTCS stack? By the way I am using MQX 3.8. I hope I won't have to upgrade my version if it is not a known issue in 3.8.

Thanks.

0 Kudos

336 Views
Martin_
NXP Employee
NXP Employee

Hi vines,

If you're using shutdown(socket, FLAG_CLOSE_TX) I think it means the TCP connection will be kept for DEFAULT_TIMEWAIT_TIMEOUT until tcp socket moves to CLOSED state. By default this is about 2 minutes (see rtcs.h for this definition) if I remember correctly.

Try to put 2 minutes delay between step 3 and 4.

0 Kudos