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!

Subscribe
Notify of
guest

252 Comments
Inline Feedbacks
View all comments
Graelynde
Graelynde
2 years ago

I’m really not sure why mistlands has made this issue so much more prevalent. I’ve been playing on different servers hosted by myself and friends for over a year now and we have never had lag issues this bad before. I hope the devs change their tune about this being an ‘all the fault of those connecting to your server’ issue. I’m still playing with the same 5-6 people.

Gesper
Gesper
1 year ago
Reply to  Graelynde

Yes, our entire team can confirm this, having completed Mistlands some time ago.
The general performance has gotten “significantly” (compared to the previous game version) worse again with mistlands-update starting with 3-4 players in one area.

It is as James has discussed with us several times here in the comments and after all this time with this knowledge, my observation and logical conclusion confirm this even more:
After each update there is more data to submit, while the server model has remained largely the same.

The guide here definitely helps make the multiplayer experience more enjoyable, but it doesn’t solve the problem. The problem is the code. Most noticeable is the positive improvement in the camp and when exchanging goods via containers, but also when fighting in groups, you notice a slight improvement – we compared it directly. It is definitely noticeable immediately when the .dll has been reset by an update. Although at first glance the procedure seems to bring little improvements, it is immediately noticed when it’s gone and you want it back immediately. So I recommand this to everyone.

What we also noticed are extremely annoying and heavy reload jerks since Mistlands, but this is another/new problem, which could be related to the world save.

I am shocked at how few players even complain about all this, how few notice it. Does anyone even play a co-op multiplayer game with multiple people? Have the standards of today’s generation of gamers dropped so low? Or are most players unable to recognize/understand these types of issues? Really, our entire group logged in, played for 5 minutes, and everyone noticed right away. Whatever is going on with the rest of the player base is beyond me.

You have to keep in mind:
We are talking about a problem that if I open a normal server through GPortal and play the game “normally” without mods (so as intended by the developer), that without modifying the .dll, it is no longer playable/or at least not enjoyable for all of us when there 4-5 players are in an area. The game has sold millions of copies and been successful for 2 years. You can’t explain that to anyone with normal common sense.

Anyway, we had our fun thanks to this fix.
I don’t know how I could have enjoyed – in particular mistlands now – if the guide didn’t exist
Thanks to James again.

Best regards/

K S C
K S C
2 years ago

I’m a complete and utter moron, and DNspy is quite frankly far far beyond my ken. Can someone just, “make” a DLL that isn’t strangled down to dialup bandwidths, and upload it please? 🙁

Sketaful
Sketaful
2 years ago
Reply to  K S C

I’m going to try to see if this fixes my problems. If it does, I’ll upload the .dll for you to http://sketaful.se/valheim/dll-fix/
That page will just list whats in it and I’ll upload every new one I make when I make it, so it might be a day or two late sometimes.

What James is talking about with the “effort” of compiling etc… Well, as long as it’s already being complied since I need it for my own server, it’s really just an two-click upload to share it. 😉

Sketaful
Sketaful
1 year ago

The files will be maintained atleast as long as I have my own server up since it’s basically just uploading what I already do every time it’s needed. It might take me like a day extra sometimes if I’m working, but beside that it will be there.

Sketaful
Sketaful
1 year ago

When it come to the legal aspect of uploading the files. Yes, it’s the property of Coffee Stain Studios but it’s not the actual game I’m uploading so either they care enough to tell me to pull it down or they don’t. I’m not hurting their business by hosting the files, I’d say I rather do the oposite. And Sweden. Lovely country. The effect a negative response from them would do is that the files will be removed (by me if they ask me to), they won’t press charges, and if, for some strange reason they will, they won’t win since they can’t prove it hurt them in any way.

Btw. I live like 7km (around 4 miles) from their studio so I guess I just head over there and ask them why if they do. 😛

Sketaful
Sketaful
1 year ago

Yeah, I wouldn’t hold my breath. They’ve made an amazing game but it’s not very well optimized.
I wonder, Does it help to do this not only on the dedicated server but on the game itself?

Sketaful
Sketaful
1 year ago
Reply to  Sketaful

…and is there any difference between the game’s assembly_valheim.dll and the one on a dedicated server or is it enough to use it in both?
Also. While at it, changing false to true in a couple of places let you use the odins dlc ;P

Sketaful
Sketaful
2 years ago
Reply to  K S C

You will have to keep a view on the date of upload to figure out if it’s new enough.

FreezeLag
FreezeLag
2 years ago

Hello everyone! Just wanted to add our experience if anyone else oversees this problem when it’s not working to modify the file if the serverhost has a linux system and sent the .dll file to a windows user to quickly modify it and send it back.

We tried to just send the “assembly_valheim.dll” file over and change the values before sending it back and overwrite the old file in the servers folder. This made alot of errors popping up in dnSPY when you tried to compile the file after the numbers were edited. Turns out you need to zip/rar the whole “Managed” folder with all the .dll files included and send it over for it to work as it seems to be looking for other dlls mentioned in the code when you compile it. This might be obvious to an experienced user but we totally overlooked it before we finally found out why it didn’t work. It did work like a charm after we had the whole folder on the computer editing the file though, afterwards we just zipped the whole folder down again after changing it and sent it back to the linux server system for replacment.

And thanks for guide James! Super helpful and easy to follow, cheers!

~Charlie

Sketaful
Sketaful
2 years ago

It’s the same for windows as well. It needs the whole folder to get it all correct. It looks for dendency files and if it doesn’t find them where it expects them it’s a ton of errors.

orbtl
orbtl
2 years ago

Hey James, I’ve got another question. I was reading through an earlier thread you were responding back and forth in about how the netcode is really P2P, and a lot of the issues stem from the fact that tons of data regarding monster behavior and such have to be sent back and forth between the player that is “controlling” the monsters with their client, the server, and the other players. This got me wondering:
Should this data rate change you document in this article be made to the client application for all the clients playing, in addition to the dedicated server?

Say my client is the one in charge of the monsters and such and I have 5 friends in a dungeon with me. Isn’t my data rate cap on my client going to get hit and cause lag issues? Wouldn’t it help to raise that cap on my client, and not just the dedicated server?

Please let me know if I’m missing some key piece of information here, such as the data rate being set separately in code for the client than for the server, for example

orbtl
orbtl
2 years ago

Okay thanks, that makes a lot of sense. I’m currently experiencing a lot of lag issues even with the increased limits and it seems like something else is limiting the data rate, and I’m not sure what. Was hoping maybe you know. I increased the values to 10x instead of the 3x recommended here, and I noticed in our base with a few players connect I’m capped out at data sent: 150kb/s. I have symmetric gigabit internet so I can handle way more than that, so I tried upping the number to 100x and it still won’t send more than 150kb/s. Do you know if there is some other variable somewhere that is limiting my data sent so that my client (when the chunk master) can send more data to handle all of the physics going on?

Russ
Russ
2 years ago

This doesn’t seem to work for my Linux Dedicated server. The server crashes shortly after starting. Do you have any hints on how to properly edit this file for a Linux Dedicated server ?

Russ
Russ
2 years ago

Thanks for the info. I pulled the files directly from the Linux server, as it is also in my office. But I initially had the Windows files open from my own installation on my PC, where I was editing, so I think I was a victim of the cached files. I appreciate your insights. Thanks for the guide!

orbtl
orbtl
2 years ago

Hello! Thanks for this wonderful guide. I remember following these modifications over a year ago and they were very helpful. However I have just returned to the game and it seems like the code has changed. Do you know if there’s a way to still make these changes with the changes to the netcode (or if they are still necessary?). The SendZDOs method that contained the code you modified appears to have been rewritten and no longer contains that hard-coded integer value you were modifying. Here is what I see in the code now:

private int SendZDOs(ZDOMan.ZDOPeer peer, bool flush)
{
if (!flush && peer.m_peer.m_socket.IsSending())
{
return 0;
}
float time = Time.time;
this.m_tempToSync.Clear();
this.CreateSyncList(peer, this.m_tempToSync);
if (this.m_tempToSync.Count <= 0)
{
return 0;
}
int num = this.m_dataPerSec / 20;
ZPackage zpackage = new ZPackage();
ZPackage zpackage2 = new ZPackage();
int num2 = 0;
for (int i = 0; i num)
{
break;
}
}
}
if (num2 > 0)
{
zpackage.Write(ZDOID.None);
peer.m_peer.m_rpc.Invoke("ZDOData", new object[]
{
zpackage
});
}
return num2;
}

orbtl
orbtl
2 years ago

Thank you so much. I guess dnSpy keeps a cache of files you have opened previously or something? Once you pointed out that this was actually the old code, it made me realize this must be the case. I made sure to hard close everything in dnSpy and re-open the .dll again and the new code showed up as it does in this article. Sorry for my mistake and thanks so much for quickly helping me figure out what was wrong!

Patrex
Patrex
2 years ago

Hello, im running a dedicated server and i have some of those sync/lag issues. We are 4-6 people playing constantly.
Sometimes doors and chests take a long time to open. The ennemies/animals look like they are teleporting.
Do you think its a CPU/RAM issue or more like the problem with server’s send/receive limit ?
Is this post still valid in november 2022 ?

Thank you!

Patrex
Patrex
2 years ago

Thanks for the detailed reply.
Yesterday we were 9 on the server and i noticed that the problem is only when we are in the same sector of the map.
If i take a portal to another continent far away, i dont have any sync issues.
But as soon as i come back to our village, i can see the problem (Mobs teleporting, delay on doors, chest etc)
Only with 3-4 person in the village and its very intense, cant even fight a group of Greydwarves
Its kinda sad since the point of multiplayer is to play in group with friends…
I also found a post from one of the Devs who said the game is not using a P2P network :
I’ll still try your fix tomorrow and we will see if its a little better at least, but i hope they fix the code soon…

Gesper
Gesper
2 years ago

Hello James and Patrex,

A small status update from Mistlands:

I upgraded our server to Mistlands too. We are now up to 6 players and tried playing without this fix at first within the new update.
We encountered still the same problem, nothing has changed with regard to the lag problems.

My hope was that when Mistlands drops for the console release, the main focus of the developers will be on the lagfix and that was also the reason for the long waiting time. But I was wrong. I admit I am a little disappointed what they have been doing all this time aside from implementing a few new models (/no hate).

Anyway, I applied all changes from this fix again on my server assembly_valheim.dll and got the same results as before the update. A lot more playable/enjoyable especially when player 4 and 5 hits the same area and I would recommand this fix to everyone -> Big thanks to James. I repeat: The lag problem is still! exactly like James figured it out on comment page 3 in our discussion.

I also recommand for all server hosts to ensure that every client has done its FPS boost (boot.ini configuration, disabled steam overlay, plays with “-window-mode exclusive -screen-fullscreen” and sets Task Manager priority to high). In case there are players on the server who are not playing on a high end device, it’s better to avoid extreme FPS drops.

Remember: The lag is as bad as the weakest link for all players in one area.

Best regards and keep up the good work!

Sketaful
Sketaful
1 year ago

It’s basically a P2P game with one server between that only have one job, to register all the changes made to the map when you’re offline. You could basically have it as a P2P game which keeps the latest mapchanges on OneDrive so everyone downloads that when starting their games. 😛