Revisiting Fixing Valheim Lag – Modifying Send/Receive Limits

My Valheim Dedicated Server Castle
My Valheim Dedicated Server Castle

I’ve previously covered fixing Valheim’s dedicated server lag by modifying the dedicated server’s send receive limits. Since I wrote that article not long after launch though the game has been updated that the old method no longer applies. I had theorized in the comments of that article how to address this and today Sam R. confirmed that this method did indeed alleviate most congestion on the server in the places that the send/receive limits are known to bottleneck.

This updated guide will show some updated methods you can use to help reduce lag on your Valheim dedicated server!

Symptoms of lag on Valheim dedicated servers

The symptoms you are experiencing on the server can help narrow down what type of lag you are experiencing and what may be causing it. Lag caused by your CPU being too overloaded for example will have different symptoms than lag that is caused by your server hitting the maximum send/receive limit as an example.

By far the most common type of dedicated server lag seen in the early access Valheim release so far has been mainly due to the low send/receive limit if 64KB/s that is hard coded into the server. This means that when you do something like take your character a heavily populated area on the server it has to send all that information about all the buildings/players through an artificially limited very narrow 64KB/s pipe.

Symptoms of the send/receive limit type of lag include things like chests taking a very long time to open, sometimes 10-30 seconds or never opening while at the same time being able to see players walking around normally. This is because the other people’s X / Y / Z positions is a very small amount of data compared to all the items/names/attributes that are stored in the chest. The other players moving easily fits through the narrow pipe while the chest full of loot’s data chokes since it is much larger.

I’ll specifically lay out how to modify your server and increase this limit in the “Modifying your server’s send/receive limit” section below. Before that though let’s cover a more specific type of lag to watch out for (mostly by being avoided) related to terrain manipulation.

Avoid extensive terrain manipulation (for now)

Another commonly observed source of lag has to do with terrain manipulation. Some players have found that if they take a big chunk out of the ground in Valheim and make extensive terrain modifications that the area gets filled with separate “instances” of terrain for each modification people make. Think of the original ground as one big piece in the game files. Valheim has the very cool feature of letting you modify terrain by raising it/lower it/level it/etc. Each time you do this though the game has to store what you did as a new “instance” or a modification.

The way Valheim seems to work now is it renders that original piece and then the modifications afterward. Early reports suggest having a whole bunch of them in one spot seems to sometimes cause everyone’s in-game FPS on the server to drop as low as 40 when the clients normally are much much higher than that.

The best way to avoid this type of lag at the moment is to limit how much terrain modification you are doing in areas near your base. It is likely this will be addressed/eased in future patches. There should be some things the developer team can do to clean up/merge these instances or at least reduce their effect on the rendering process so it doesn’t drop the in-game FPS so dramatically.

Modifying your server’s send/receive limit

At this time there is no options file for Valheim unfortunately. The only way to change it is to modify the server’s code and recompile the module that controls this.

Fortunately this is a lot easier than it sounds because the module we need to change is in .NET instead of something like C++ which would be much more difficult to do this with. I did not come up with this method, a very clever user on reddit (maximgame) shared it here and I want to give credit where credit is due!

Despite it not being too difficult these modifications are at your own risk. I haven’t experienced any problems so far but technically we are modifying the server’s code with the following procedure so understand that this isn’t an officially supported method and could have side effects or if you make a mistake could cause serious problems. Make sure you have backed up everything (your game world files especially) before you try anything!

Another important note is that it’s very likely you will have to make the changes again each update since the file we are going to modify is going to get replaced by updates. Until they add either a launch option or options file to officially change/control this property that is unfortunately unavoidable.

With all that being said if you understand the above and still feel comfortable let’s proceed!

Get dnSpy utility

We are going to use the utility dnSpy to modify the server code and recompile the module. The utility is free and is available here (download the Win64 build or dnSpy-net-win64.zip): GitHub dnSpy Official Page

Extract the archive and run the program dnSpy.exe from inside the folder you extracted. The dnSpy application will open.

Modifying server with dnSpy

We are going to go to the “File” menu in the top left and choose “Open” like this:

dnSpy - File -> Open Menu
dnSpy – File -> Open Menu

Now you need to navigate to the Valheim dedicated server folder which is located in your “steamapps” folder. If you left all the options default when you installed it’s typically “C:\Program Files (x86)\Steam\steamapps\common\Valheim dedicated server\valheim_server_Data\Managed”.

We are looking for the file “assembly_valheim.dll” in that folder:

dnSpy - Select File
dnSpy – Selecting Valheim server assembly file

Select the file “assembly_valheim.dll” and click “Open” and the server’s assembly will load into dnSpy.

Once it has finished loading we are going to go to the “Edit” menu and click “Search Assemblies” like this:

dnSpy - Search Assemblies
dnSpy – Search Assemblies

For the search criteria enter “ZDOMan” like this:

dnSpy - ZDOMan Search
dnSpy – Searching for ‘ZDOMan’

After the search completes you should see a list of results like the one above. Double click on one of these entries on the list and it should place you in the ZDOMan section of the assembly (seen on the left hand side of the screen):

dnSpy - 'ZDOMan' section of assembly
dnSpy – ‘ZDOMan’ section of assembly

Perfect. We are now in the right place to make the changes. We just need to locate the line that has the send/receive limitation. To find that press Ctrl+F to bring up the find box (or choose “Edit” -> Find from the menu) and search for:

 sendQueueSize

which should bring up the following lines:

dnSpy - sendQueueSize Variable
dnSpy – sendQueueSize Variable

If you are having trouble finding the lines make sure you’re using Ctrl+F to search this time and not the “Search Assemblies” from earlier at the bottom of the screenshot, that’s a different type of search. Make sure you’ve disabled the case sensitive options (the Aa right below the box) or any other search filters. Your search query box should be in the top right of the screen like the above screenshot that has sendQueueSize in it.

Now right click on this line:

int num = 10240 - sendQueueSize

and click “Edit Class (C#)…”. Another window will open that will let you make changes to the file. We are going to modify ‘10240’ to a variable of our choosing. I made mine 30720 since after doing some conversion math that is the equivalent increase from my previous launch version guide that provided great performance and I kept them the same. If you followed my last guide this will give you the exact same performance as the old one did.

There are two instances of this variable. The second instance is 3 lines above the highlighted line we searched for and is:

if (!flush && sendQueueSize > 10240)

Modify both instances of 10240 to 30720.

Theoretically setting a limit too high could cause the server to swamp your internet connection if you have a large number of players on and there’s essentially no limit but there shouldn’t be any other effects of a very high number.

Once you’ve changed both numbers to the desired value we need to press the “Compile” button in the bottom right corner of this new window. This window will close and take you back to the previous one.

Saving the new assembly

It’s time to save our changes into a new .dll file. Choose “File” -> “Save Module…”:

Saving New Assembly
dnSpy – Save Module as assembly_valheim_modded.dll

Change the file name to assembly_valheim_modded.dll. Don’t overwrite the original file just yet as we will want to take a backup and make sure that the server is closed before we do that. Press “OK” to write the new assembly_valheim_modded.dll file.

Backing up original file and replacing with our new one

Now it’s time to back up the original file and replace it with our new modded file. First make sure your dedicated server is closed otherwise the dll file will be in use. It is also a good time to make a backup of your world file just in case (see my Valheim Dedicated Server Backup Location / Guide for instructions)

Navigate in a file explorer window to your Valheim server directory where we saved the new assembly file (usually C:\Program Files (x86)\Steam\steamapps\common\Valheim dedicated server\valheim_server_Data\Managed).

First make a copy of the original assembly_valheim.dll file. You can just select the file and “copy” then paste a copy of it in that same folder and Windows will make a “valheim_assembly Copy(1).dll” file for you or you can back it up to a separate folder. Now remove the original file and rename your “valheim_assembly_modded.dll” file to the original file name of “valheim_assembly.dll”.

That’s it, you’ve now replaced the old assembly with the new one! Go ahead and start your server normally and the changes will have taken effect.

Other Resources

Make sure you are taking constant backups of your Valheim dedicated server: Backup Valheim Dedicated Server – World Folder Paths / Guide

It’s also important to back up your local game’s characters and local worlds if you are using them. I have written a guide for the local game side that is available here!

For historical reasons I’ve left up the post with the old method here as it helped me devise the new method: Fixing Valheim’s dedicated server lag by modifying the dedicated server’s send receive limits

Other Valheim Lag Issues

If you know of other Valheim lag issues that I didn’t cover here or are still having problems let me know in the comments and I will update this guide with other tips/tricks and try to answer any questions. It’s still a very new game and there isn’t very much out there for it yet. Have fun and enjoy!

252 thoughts on “Revisiting Fixing Valheim Lag – Modifying Send/Receive Limits”

  1. Avatar for skillzilla

    У меня сервер на ubuntu, загрузил с сервера файл assembly_valheim.dll , при компиляции получаю коды отладки и не могу сохранить класс. Если же редактирую assembly_valheim.dll из папки с игрой (локально на своем компьютере), то файл компилируется нормально, но когда загружаю его на vh dedidacted server, то при запуске сервера получаю ошибку:
    "дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Peak Block count 1
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Peak Allocated memory 26.3 KB
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Peak Large allocation bytes 0 B
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: [ALLOC_TYPETREE_THREAD]
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Peak usage frame count: [16.0 KB-32.0 KB]: 103064 frames, [32.0 KB-64.0 KB]: 2999 frames, [64.0 KB-128.0 KB]: 3101 frames
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Requested Block Size 2.0 MB
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Peak Block count 1
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Peak Allocated memory 120.1 KB
    дек 04 18:51:10 ubuntu-test valheim_server.x86_64[30843]: Peak Large allocation bytes 0 B
    дек 04 18:51:10 ubuntu-test systemd[1]: vhserver.service: Succeeded."

    Буду разбираться. Автору статьи спасибо!

    1. Avatar for James A. Chambers

      Hey skillzilla,

      Yes, I know exactly what the problem is. You cannot edit the Windows files and then upload them to a Linux machine. The files are different even though they won’t look that different when you’re editing them.

      What you need to do is download the files off your Linux dedicated server onto Windows and edit those ones. Don’t try to edit the Windows ones and upload it to the Linux machine. Lots of people have tried editing their DLL file for the Windows dedicated server and uploaded it to Linux with the same result. They’ve already fixed it though by editing the Linux files instead of the Windows ones (which you can still do on Windows, but you need to transfer the whole folder of files for everything to load correctly).

      Hopefully that helps!

  2. Avatar for TFL

    Hi.

    Does this still work with patch 0.217.31? I tried applying the fix (I’ve done it before), but this time it looks like stuff in the code got moved around, and since I’m not a coder it’s just shooting blind. I know there are a couple of network fix mods available, but they also seem to have issues hence I’d rather apply this fix on the server myself.

    1. Avatar for James A. Chambers

      Hey TFL,

      I’d be really surprised if they redid their networking code. We were begging them to change it for a long time but I think most people have given up or moved on / worked around it. Some people are using the mods which do a very similar thing to this article as you’ve pointed out.

      I see Sketaful’s precompiled binaries are now a broken page. I tried to warn him that seriously trying to recompile every single patch they ever release for this game was totally unsustainable and that the second he was no longer playing Valheim anymore that he’d go inactive. The response I got was that it’s super easy to make and that he’d keep doing it (he didn’t). I’ve removed that link/information from the article as of today.

      I got a lot of push back from not providing precompiled binaries but this is exactly why. It actually is unsustainable as I’ve maintained from the start. I don’t blame Sketaful for not coming back here and letting me know he was going to stop because I told him that’s exactly how this was going to end and he disagreed with me. Those links have probably been dark for months but I just hadn’t noticed until I got a couple of posts like this saying something wasn’t working.

      How could I have known exactly how the whole precompiled binaries thing was going to play out? It’s easy. So easy. If you were no longer playing Valheim at all why would you keep downloading every single minor patch / bug fix on your own time (for free) and fixing Valheim’s game for them while they make millions of dollars? That’s an easy commitment to say you’re going to make while you’re actively playing as you need the mod.

      He actually tried to tell me he was going to keep maintaining this while not playing. Why would anyone do that? It was such an easy call to make. People just aren’t being honest with themselves here. You’re literally doing a very successful paid game development studio’s job for them for nothing. People think when they’re not actively playing they’re actually going to do that *WORK*? Incredible. Laughable. I said the exact same thing at the time but told him as long as he’d do it I’d link to it. It really didn’t last long.

      I’m so sure that Valheim hasn’t done a *thing* with the network code that I actually downloaded the Valheim dedicated server and opened it up using the *exact* instructions from this article:

      // Token: 0x06000C42 RID: 3138 RVA: 0x0005D9BC File Offset: 0x0005BBBC
      private bool SendZDOs(ZDOMan.ZDOPeer peer, bool flush)
      {
      int sendQueueSize = peer.m_peer.m_socket.GetSendQueueSize();
      if (!flush && sendQueueSize > 10240)
      {
      return false;
      }
      int num = 10240 - sendQueueSize;
      if (num < 2048) { return false; } this.m_tempToSync.Clear();

      Right there in ZDOMan. Unchanged. They haven't changed anything since I wrote this article so I have to wonder if you are opening up the client file instead of the dedicated server? You should navigate directly to:

      C:\Program Files (x86)\Steam\steamapps\common\Valheim dedicated server\valheim_server_Data\Managed

      That can't be the file you are opening if you can't find ZDOMan and the code in question from the article.

      For anyone else reading this yes, as of December 3rd 2023 nothing whatsoever has changed with this. I'm not expecting it to change either. If we could get the Valheim developers to change / fix the networking code then this post would have fallen off the front page of Google years ago. With all of the screaming the players have been doing since almost day 1 on this issue with zero resulting changes I expect this method will continue to work for years to come if not forever.

      It's unfortunate but when it comes to the networking code you should bet against Valheim fixing it every time. It's a reasonable thing to wonder if things had changed since this guide was written (for most games). For Valheim though it would be an absolutely earthshattering event if they fixed the networking code and unfortunately it will require a *complete* overhaul.

      That's why I don't think we'll see it. I think they'd choose a different multiplayer design for Valheim 2 but I'm not expecting them to gut the original Valheim at this point and completely rewrite the networking code. I think we're stuck with what it is.

      The time to change it would have been before Hearth and Home, before Mistlands, before a whole lot of new stuff was added on top of what was already there (which yes, we were already doing this mod/hack and asking the developers to properly fix it long, long before Hearth and Home let alone Mistlands). The more they add the harder it is to go back and gut the core networking code because now so many new systems are added on top of it that depend on it working the way it is.

      If it was going to happen it should have happened right after release when they realized it was a big mistake to use that type of networking code/system in a game like this. Every day that passes it gets harder and it has now been YEARS of moving in the wrong direction. We couldn't even get them to make the changes we are making in this article (which isn't a fix, it's just a workaround that squeezes out enough improvement to let most groups/parties actually finish the game and not lag out during the big boss fights).

      Truly fixing it would require the Valheim developers (or someone else via a heavily reworked dedicated server similar to Spigot/Paper/Forge in the Minecraft server world with client-side mods also required to remove the P2P/Peer-to-Peer elements in the current system) to redo the networking code entirely.

      1. Avatar for TFL

        Hey thanks for the quick reply. I’ll have to take another look at it tomorrow, see if I can’t figure it out. I must have done something wrong then, since my search for ZDOMan wasn’t fruitful 🙂

        1. Avatar for James A. Chambers

          It’s extremely common if you Google it and it has nothing to do with Valheim specifically. You have a bunch of conflicting development libraries installed. You will need a cleaner system to rebuild this or you will have to heavily modify the Valheim code to specify which “Debug” routine to call. Valheim just calls it without specifying it which means if you have multiple ones installed you’ll get this error!

          Ambiguous means that since the Valheim developers didn’t specify *exactly* which debug to use since you have multiple ones it has no idea what to pick. You’d have to either change the code (by going in and changing all of those debug calls to the exact debug routine it should be using) or use a Windows installation without this extra non-standard stuff on there.

          You can be sure whatever library you have installed on your system is *not* one the Valheim developers had or they would not be able to build the game. It’s coming from either a game installing the development headers or you actually do have Unity developer engine installed or something similar.

    1. Avatar for James A. Chambers

      Hey Thomas,

      There are a few others in the comments that had similar issues but needless to say something is not quite right. We’ve seen it be permissions issues (where you needed to be admin), people trying to edit the files while the server is running / in use, and a few other things. If you look through the comments here you should spot a solution!

        1. Avatar for James A. Chambers

          Looks like you have conflicting libraries installed on the system. I’d imagine you have the Unity engine development headers installed by the look of it or some other development library that also uses that same “Debug” namespace.

          I have no idea how to fix it. If you do a clean install of Windows it won’t happen. You have other libraries on there that are conflicting with the code that I do not have installed (or most others).

          If you are actually a developer (or pretty tech savvy) then spin up a virtual machine to do this with a clean Windows install. I have no idea how to fix your system or headers to not have this crap in there that is conflicting with it.

          In other words if you are getting these issues your system is way too “dirty” to recompile this. You have a bunch of other stuff installed that is conflicting with the code the Valheim developers used (which doesn’t specify which Debug to use unfortunately so if you have multiple libraries using that you won’t be able to recompile).

          This is par for the course for compiling code unfortunately. Developers have to have a very specifically configured environment to build their product. Fortunately the requirements for this are pretty limited but your system’s libraries are way too trashed to rebuild this dll it looks like!

        2. Avatar for Hark

          I assume you have either figured it out or moved on but for anyone looking for how to solve this in the future what I did to fix this was while editing in C# used ctrl + F and typed Debug and then used the arrow to the left of the text box to access the replace function and replaced all instances of Debug with UnityEngine.Debug

  3. Avatar for Renan

    Hey James!! Thanks for your article!! Do you know if there’s a problem if i increase this variable to the double of 30720 ? like 61440 ?

    Thanks in Advance!!

  4. Avatar for lee

    heya James!
    i’ve been playin since launch. the mod ‘better netwoking’ led me here. i learned a bit reading your post. thx for sharing thy observations. i love valheim, wish they’d fix the networking. ~ lee

  5. Avatar for Kyp

    Howdy James,

    So I think I understand the issue/what the fixes are targeting, but I was interested to know if you know a bit more about what the development team could do on their end to optimize the networking? IE; how does a game like Minecraft, or Ark, or any modern game with dedicated hosting abilities handle the throughput so much better, even using P2P connections? Where is the achilles heel so to speak, and is this something the team could fix at all, or would it require a disproportionate amount of resources/rebuilding from the ground up?

    I’m not educated on network engineering in the slightest but find it pretty fascinating, so any information I don’t already have would be wonderful!

    1. Avatar for James A. Chambers

      Hey Kyp,

      Great question! So the biggest issue is that random players will “own” each zone they are in and control all of the monsters in that zone. That means you will only be as fast as your slowest player’s connection. Other games don’t typically “wait” for data from players even if they have P2P components.

      Some of the things that would need to be completely redone are collision, projectiles, monster zones and a whole bunch of other things to not work this way. It would honestly be easier to start from scratch I think on the networking rather than fix it as crazy as that sounds. The issues are fundamental if that makes sense and it’s hard to fix fundamental issues without starting over.

      Hopefully that helps!

  6. Avatar for Zeravnos

    Hey James,

    Came across this article after I had already invited 8 or so of my friends to my shiny new self-hosted server before realizing Valheim’s capacity shortcomings. While imperfect, this mod DOES help a bit with the rubberbanding and general lag with so many online, I think maybe we might just have to deal with ‘acceptable’ at this point.

    Though something I did notice, is even after applying this modification and verifying that it did produce good results with about 5 people, once we got to around 8 people online, it looked like our send and receive speeds kept dipping below 30 and the lag started to come back. Is this normal for a larger set of players? Is the rate distributed amongst people in the server?

    Thank you so much for this helpful writeup and the work you’ve done!

    1. Avatar for James A. Chambers

      Hey Zeravnos,

      Thank you so much for your kind words here, and you are most welcome! So I think what you are describing is about right. This mod buys you about 1 additional player slot before the problem starts.

      It gets worse every update. If you read through the comments here this is a pretty universal sentiment. At launch this mod would probably get you up to 6 roughly. It’s probably about 5 now. Without it it starts lagging at 3-4.

      The reason for this is that each update they add new features and data. For example this update has a new stat (Eitr or basically mana/magic). That’s all additional data that is being sent around that didn’t exist at launch. This system was already woefully inadequate at launch but it was less taxed than it is now after all of the updates.

      In other words this workaround is basically like using a band-aid to close up a wound that actually needs several stitches. It’s not going to be enough. The Valheim developers need to go in there and do some serious operating and then stitch the patient back up. That’s not going to happen though. It has been this way since launch and it has only got worse unfortunately.

      I gave up hope for them fixing this a long, long time ago. I’m not sure they’re even capable of it or that they even understand the problem. If it’s ever fixed it will be via player mods that completely throw out Valheim’s networking code. It has to literally be the worst multiplayer code ever released rivaling like old original Xbox games that were purely P2P.

      It’s actually worse than those old Xbox P2P games because Valheim uses a bastardized mix of P2P and standard client/server multiplayer models that literally make no logical sense. Instead of the advantages of one or the other since they mixed them it has the disadvantages of both.

      It’s like mixing a bunch of flavors together at the soda fountain. If the flavors aren’t complimentary such as lemon lime with a specific other compatible soda (and most of them won’t be) the taste you get will *not* be better. Most of them will clash and be *nasty* and that is exactly how I would describe Valheim’s networking code and how they mix multiplayer models. You end up with the disadvantages of each multiplayer model and there ends up being no advantage. They’re behaving like children trying to mix a bunch of soda flavors together with absolutely no understanding of which flavors can go together thinking it’s going to end up awesome with superpowers from all the different flavors. Then it turns out super nasty which any adult that has the slightest clue what they’re doing could have told them. That’s how I see them.

      Hopefully that helps!

Leave a Comment

Your email address will not be published. Required fields are marked *

Type here..

Exit mobile version