Server memory methods

From VbGORE Visual Basic Online RPG Engine

This article is aimed towards explaining the methods used by the server to use as little memory as possible.

Map cleaning

Maps that do not have any users on them are unloaded from memory, along with the NPCs on them. This rate is defined by the following variable:

<vb> Public Const EmptyMapLife As Long = 180000 '3 minutes </vb>

Along with this, objects that remain on the ground of maps for an extended period are deleted. This is not only for memory cleaning, but for cleaning up the garbage on the map. This object cleaning is defined by the variable:

<vb> Public Const GroundObjLife As Long = 300000 '5 minutes </vb>

When a map is unloaded from memory, all of the objects and NPCs on it will be deleted from memory, too. The objects array will be erased since that is part of the map's data array, but the NPCs are stored in a global array, so they will attempt to be erased. Since only the highest unused NPC index can be erased, the slots that are cleared will be set aside for recycling. They still take up RAM, but the next NPC to be created will take up one of the cleared slots. This still benefits us, though, since we do not have to allocate memory to place the new NPC.

Both of these unloading rates (objects on the ground and empty maps) are checked every X milliseconds, where X is the variable:

<vb> Private Const UpdateRate_Maps As Long = 30000 </vb>

This means that every 30 seconds, the server will check to see if any maps or ground objects need to be erased. All this number does is state the time accuracy. A higher number means less checks, which equals less CPU usage. Having a high accuracy of memory unloading isn't too important, so 30 seconds works fine.

Object data cleaning

Objects, when grabbing their data from ObjData, use a bit of an abstract calling method. This is because of the class module wrapper around the ObjData. Every time you request information about an object from ObjData, the following events happen:

1. A check is done to confirm a valid index is passed (to prevent crashes) 2. The object is checked if it is loaded yet - if the object is loaded, skip to step 5 3. A check is made if there are any unused indexes - if so, the first unused index is used, if not, the array is resized to fit the new object data 4. The object data is loaded from the object's .temp file 5. The object's life timer is set to x milliseconds, where x is the constant ObjMemoryLife

This means that you do not have to worry whether an object is in memory or not when you call it. Also, every object will remain in memory for ObjMemoryLife milliseconds after any data was grabbed from it the last time.

Like the map unloading, objects are checked only after a bit of time has passed to reduce CPU usage. This time is defined by:

<vb> Private Const UpdateRate_UnloadObjs As Long = 120000 </vb>

So every 120000 milliseconds (2 minutes), the server will check if an object that is in memory hasn't been called for ObjMemoryLife milliseconds. If so, it is unloaded.

Related articles

Personal tools