Am I going crazy? Big memory leak in NetworkTransport.Send()?

I’ve spent weeks looking through my code for a big memory leak that’s been crashing game servers. And I’ve pulled out all of my code and was left with this… NetworkTransport.Send()!! one of the most commonly used functions in the game is eating memory roughly at the same rate that data is sent… which leads me to think that maybe the sent data isn’t being released.

Maybe I’m worng, so please let me know if this code leaks for you as well. I’m really just frustrated at the time wasted if one of the most important functions wasn’t smoke tested. Thanks.

public class test : MonoBehaviour {

    int m_MyHostId;
    int m_PeerId;
    byte error;

    bool connected = false;

    void Start()
    {
        NetworkTransport.Init ();
        ConnectionConfig myConfig = new ConnectionConfig();
        myConfig.AddChannel(QosType.Reliable);

        HostTopology myTopology = new HostTopology(myConfig, 10);         //up to 10 connection allowed
        m_MyHostId = NetworkTransport.AddHost(myTopology, 9999);

        m_PeerId = NetworkTransport.Connect(m_MyHostId, "127.0.0.1", 9999, 0, out error);
        if ((NetworkError)error != NetworkError.Ok)
        {
            Debug.LogError("Network error is occurred: " + (NetworkError)error);
        }
    }

    void Update()
    {
        int recHostId;
        int connectionId;
        int channelId;
        byte[] recBuffer = new byte[1024];
        int bufferSize = 1024;
        int dataSize;

        if (connected) {
            for (int i = 0; i < 2; i++) {
                NetworkTransport.Send (m_MyHostId, m_PeerId, 0, new byte[500], 500, out error);

                if ((NetworkError)error != NetworkError.Ok)
                {
                    Debug.LogError("Network error is occurred: " + (NetworkError)error);
                }
            }
        }

        while (true) {
            NetworkEventType recData = NetworkTransport.Receive (out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);

            if ((NetworkError)error != NetworkError.Ok) {
                Debug.LogError ("Network error is occurred: " + (NetworkError)error);
                break;
            }

            if (recData == NetworkEventType.Nothing) {
                break;
            }

            switch (recData) {
            case NetworkEventType.ConnectEvent:
                Debug.Assert (connectionId == m_PeerId, "Success");
                connected = true;
                break;
            case NetworkEventType.DataEvent:
                Debug.Log ("Received Data");
                break;
            case NetworkEventType.DisconnectEvent:
                Debug.Assert (connectionId == m_PeerId, "Failure");
                connected = false;
                break;
            default:
                break;
            }
        }
    }
}
1 Like

Just a guess, but instead of sending in “new byte[500]” for the buffer, try defining a global SendBuffer array, allocate it in Start() to 500, and send that into the Send function. Every time it’s allocating a completely new 500 byte long array in memory, and having it’s pointer sent to the C++ code (which may or may not release and de-allocate it). When you send a reference to a global array variable, you are sending the same memory address(s) every time for it to use, It will not matter if the C++ code releases the memory because it will only ever use those 500 memory addresses every single Send call.

Just tried and still increasing at the same rate… I’ve even put in a System.GC.Collect() to make sure it wasn’t the garbage collection fooling around.

Hope we can get a fix for this as soon as possible, since my game is live now and servers are chewing through memory left and right. With 64 players online at 1-2MB server upload speeds it doesnt take long.

1 Like

If you post your bug report # here it will have a higher chance of getting looked at soon.

https://fogbugz.unity3d.com/default.asp?923903_slu45gck0ne295uk

Hope this will help, and I’ve tested this leak - it’s in everything from at least 5.6.1 to 2017.1 beta10. Can’t get around it no matter what the network settings are configured to. Really hope for a quick fix.

1 Like

Ok I did some more testing. Had to go back to v5.6.1 without the leak… then I tested v5.6.1p2 it has the problem and everything after that too. So it could be patch 1 or 2 where it started. Hope this will help @aabramychev solve the issue.

1 Like

If Alex looks into this it will be great. However, next to this thread, you should really create an official bug report ticket for this as well. This assures that unity QA looks into this.

1 Like

Sorry for no response guys… was busy. Will take a look today and will report about result here

1 Like

Thank you Alex! The leak starts slow… seems related to the size of the Send/RecieveQueue. But once some internal buffer is filled up I guess, the memory leak grows faster.

Quick update on this issue. QA confirmed this is an issue with a successful reproduction: https://fogbugz.unity3d.com/default.asp?923903_slu45gck0ne295uk

Nice work @NongBenz :slight_smile:

1 Like

Hey there!

I wanted to inform that this problem should be fixed in 5.6.2p2.

If this problem is still present, make sure to inform us in this forum thread or by submitting a new case.

2 Likes

Any news on when it’ll make it into 2017?

+1, would like to know too

In case you’ve not seen it yet it looks like 2017.1.0f3 for the fix, so I imagine sometime next week…

This from the known issues list for 2017.1.0f2

  • Multiplayer: NetworkTransport.Send() leaks memory (fixed in f3) (923903)
3 Likes

It seems the problem is still there under linux (2018.2.5f1). Here is the result after running your code (on linux server):

I submited a bug report few days ago
https://fogbugz.unity3d.com/default.asp?1076446_eovd1p5bhhgk0520