Improving Server Stability

Joined
Aug 2, 2010
Messages
190
Reaction score
91
For those people who are interested in improving their server stability, this thread is right for you. First of all, NEXON's main servers can support 1200 players on 4 GB of RAM and runs on Quad Core (Per Channel). Obviously, our OdinMS servers can't do that. However there is hope in obtaining this form of stability. Most of us here know LightPepsi and Celino. His server has supported 600+ players using only 2.2 GB of RAM. One conclusion that can be drawn from this is that server stability is not impossible. Most importantly, the way we code affects server stability.

Some ideas for improving server stability:
Object creation and fixing memory leaks.
Danger of finalizers.
Please contribute helpful ideas and opinions on your take towards a stabler server.
 
Last edited:
Personally I love coding with this special Eclipse program which is more personalized for performance based programming.

---------- Post added at 08:26 PM ---------- Previous post was at 07:32 PM ----------

A good book to read if you want to program efficiently. I read this book, I think its a GREAT book to refer to.

jon5477 - Improving Server Stability - RaGEZONE Forums

Hmm.. thanks for the book reference. I'll probably pick one up soon and read it, since I'm starting to learn java.

Apparently the book teaches how to program efficiently, so that's a plus, since I want my server to be stable!
 
Upvote 0
Hmm.. thanks for the book reference. I'll probably pick one up soon and read it, since I'm starting to learn java.

Apparently the book teaches how to program efficiently, so that's a plus, since I want my server to be stable!

Tip code your own instead of using an instable one. You'll learn from your errors as you code and test. It will give you a general knowledge about MapleStory and it's stupid developers :).
 
Upvote 0
Unfortunately jon5477, I can't allow that book in this section. Unless you can prove beyond a doubt the author of the book allows the public to access it without cost, it falls under the category of illegal media. If you can prove it is free to the public by the author, I will undelete your post and allow the book to stay. Only reason I know this is because I also had a book in the past I wanted to share and wasn't allowed to. Sorry :(
 
Upvote 0
People who know what they are doing will not have any problems with stability. In fact, the only servers struggling with this are those which have inexperienced admins and/or a crappy hosts.

How'd you figure?
 
Upvote 0
Unfortunately jon5477, I can't allow that book in this section. Unless you can prove beyond a doubt the author of the book allows the public to access it without cost, it falls under the category of illegal media. If you can prove it is free to the public by the author, I will undelete your post and allow the book to stay. Only reason I know this is because I also had a book in the past I wanted to share and wasn't allowed to. Sorry :(

Ok.. what the hell? Jon didn't even post any "illegal media".

It was just a post with the picture of the book.. how does that fall under illegal media?
 
Upvote 0
Ok.. what the hell? Jon didn't even post any "illegal media".

It was just a post with the picture of the book.. how does that fall under illegal media?

-_- wtf... It's a PDF of the book. I already deleted the post with it. A PDF file can hold a single book, which is exactly what his post was. I know what I'm talking about...
 
Upvote 0
This post lists many tips and such which is avaible on the web. Remember that the tuning tips listed are not necessarily good coding practice. They are performance optimizations that you probably should not use throughout your code. Instead they apply to speeding up critical sections of code where performance has already been identified as a problem.



  • ArrayList is faster than Vector except when there is no lock acquisition required in HotSpot JVMs (when they have about the same performance).
  • Vector and ArrayList implementations have excellent performance for indexed access and update of elements, since there is no overhead beyond range checking.
  • For small collections ArrayList and LinkedList are close in performance, though ArrayList is generally the faster of the two. Precise speed comparisons depend on the JVM and the index where the object is being added.
    Also, clone arrays if that makes their creation faster.

  • WeakHashMap can be used to reduce memory leaks. Keys that are no longer strongly referenced from the application will automatically make the corresponding value reclaimable.
  • To use WeakHashMap as a cache, the keys that evaluate as equal must be recreatable.
  • Using WeakHashMap as a cache gives you less control over when cache elements are removed compared with other cache types.

  • Start code tuning with proof of concept bottleneck removal: this consists of using profilers to identify bottlenecks, then making simplified changes which may only improve the performance at the bottleneck for a specialized set of activities, and proceeding to the next bottleneck. After tuning competence is gained, move to full tuning.

- iKnowRight
 
Last edited:
Upvote 0
@Shawn If its illegal then we'll leave the PDF book out.

However, I don't want to disappoint people so I'll post some things that I learned from Chapter 2.
  1. Enforce singleton or static classes with a private constructor.
  2. Adding "implements Serializable" does not maintain a singleton guarantee.
  3. Avoid creating duplicate objects.
  4. Eliminate obsolete object references.

I will explain them with some detail below:
1. Enforcing singletons or static classes with a private constructor.
A singleton is a class that is instantiated ONLY once. There are two approaches towards making a singleton class. One involves a public static field, the other involves a public static factory method.

Method one:
Code:
public class Example {
	public static final Example INSTANCE = new Example();

	private Example() {
	}
}
In this case, you can easily access this class by using Example.INSTANCE then the method of which you wish to access. The lack of public or protected constructors guarantees only one class object. The advantage of this method slightly improves performance.

Method two:
Code:
public class Example {
	private static final Example INSTANCE = new Example();

	private Example() {
	}

	public static Example getInstance() {
		return INSTANCE;
	}
}
If you look carefully you will see that the INSTANCE object is no longer public but private. The advantage in this method provides you the flexibility to decide whether your class should be a singleton or not without changing the API. In essence, it makes much more sense to use the first method if you're absolutely sure that the class will forever remain a singleton.
2. Correctly maintaining a serial singleton class.
To make a singleton class serializable, it is not sufficient merely to add "implements Serializable" to its declaration. To maintain the gaurantee of a singleton class, you must also provide a readResolve method. Otherwise, each deserialization of a serialized instance will result in the creation of a new instance. To prevent this, add the following readResolve method to the Example class:
Code:
private Object readResolve() throws ObjectStreamException {
	/**
	 * Return the one and only class instance and let the
	 * garbage collector take care of the other instance.
	 */
	return INSTANCE;
}
3. Avoiding creation of new objects.
It is often appropriate to reuse a single object instead of creating a new functionally equivalent
object each time it is needed. Reuse can be both faster and more stylish. An object can always
be reused if it is immutable.
As an extreme example of what not to do, consider this statement:
Code:
String s = new String("retarded"); // NEVER DO THIS, IT CREATES MULTIPLE USELESS OBJECTS!
The statement creates a new String instance each time it is executed, and none of those
object creations is necessary. The argument to the String constructor ("retarded") is itself a
String instance, functionally identical to all of the objects created by the constructor. If this
usage occurs in a loop or in a frequently invoked method, millions of String instances can be
created needlessly.
The improved version:
Code:
String s = "no longer retarded";
This version uses a single String instance, rather than creating a new one each time it is
executed. Furthermore, it is guaranteed that the object will be reused by any other code.
Here is a more unnoticable example of what NOT to do:
Code:
public class Person {
	private final Date birthDate;

	public Person(Date birthDate) {
		this.birthDate = birthDate;
	}

	// DON'T DO THIS!
	public boolean isOld() {
		Calendar gmtCal =
		Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
		Date dateStart = gmtCal.getTime();
		gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
		Date dateEnd = gmtCal.getTime();
		return birthDate.compareTo(dateStart) >= 0 &&
		birthDate.compareTo(dateEnd) < 0;
	}
}
The isOld method unnecessarily creates a new Calendar, TimeZone, and two Date
instances each time it is invoked.
A smarter method using a static initializer:
Code:
class Person {
	private final Date birthDate;

	public Person(Date birthDate) {
		this.birthDate = birthDate;
	}

	/**
	 * The starting and ending dates.
	 */
	private static final Date DATE_START;
	private static final Date DATE_END;

	static {
		Calendar gmtCal =
		Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
		DATE_START = gmtCal.getTime();
		gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
		DATE_END = gmtCal.getTime();
	}

	public boolean isOld() {
		return birthDate.compareTo(DATE_START) >= 0 &&
		birthDate.compareTo(DATE_END) < 0;
	}
}
The improved version of the Person class creates Calendar, TimeZone, and Date instances
only once, when it is initialized, instead of creating them every time isOld is
invoked. This results in significant performance gains if the method is invoked frequently. On
my machine, the original version takes 36,000 ms for one million invocations, while the
improved version takes 370 ms, which is one hundred times faster.
Not only is performance
improved, but so is clarity. Changing dateStart and dateEnd from local variables to final
static fields makes it clear that these dates are treated as constants, making the code more
understandable. In the interest of full disclosure, the savings from this sort of optimization
will not always be this dramatic, as Calendar instances are particularly expensive to create.
4. Eliminating obsolete object references.
When you switch from a language with manual memory management, such as C or C++, to a
garbage-collected language, your job as a programmer is made much easier by the fact that
your objects are automatically reclaimed when you're through with them. It seems almost like
magic when you first experience it. It can easily lead to the impression that you don't have to
think about memory management, but this isn't quite true.
Consider the following simple stack implementation:
Code:
// Can you spot the "memory leak"?
public class Stack {
	private Object[] elements;
	private int size = 0;

	public Stack(int initialCapacity) {
		this.elements = new Object[initialCapacity];
	}

	public void push(Object e) {
		ensureCapacity();
		elements[size++] = e;
	}

	public Object pop() {
		if (size == 0) {
			throw new EmptyStackException();
		}
		return elements[--size];
	}

	/**
	* Ensure space for at least one more element, roughly
	* doubling the capacity each time the array needs to grow.
	*/
	private void ensureCapacity() {
		if (elements.length == size) {
			Object[] oldElements = elements;
			elements = new Object[2 * elements.length + 1];
			System.arraycopy(oldElements, 0, elements, 0, size);
		}
	}
}
There's nothing obviously wrong with this program. You could test it exhaustively, and it
would pass every test with flying colors, but there's a problem lurking. Loosely speaking, the
program has a “memory leak,” which can silently manifest itself as reduced performance due
to increased garbage collector activity or increased memory footprint. In extreme cases, such
memory leaks can cause disk paging and even program failure with an OutOfMemoryError,
but such failures are relatively rare.
So where is the memory leak? If a stack grows and then shrinks, the objects that were popped
off the stack will not be garbage collected, even if the program using the stack has no more
references to them. This is because the stack maintains obsolete references to these objects.
An obsolete reference is simply a reference that will never be dereferenced again. In this case,
any references outside of the “active portion” of the element array are obsolete. The active
portion consists of the elements whose index is less than size.
Memory leaks in garbage collected languages (more properly known as unintentional object
retentions) are insidious. If an object reference is unintentionally retained, not only is that
object excluded from garbage collection, but so too are any objects referenced by that object,
and so on. Even if only a few object references are unintentionally retained, many, many
objects may be prevented from being garbage collected, with potentially large effects on
performance.
The fix for this sort of problem is simple: Merely null out references once they become
obsolete. In the case of our Stack class, the reference to an item becomes obsolete as soon as
it's popped off the stack. The corrected version of the pop method looks like this:
Code:
public Object pop() {
	if (size == 0) {
		throw new EmptyStackException();
	}
	Object result = elements[--size];
	elements[size] = null; // Eliminate obsolete reference
	return result;
}
An added benefit of nulling out obsolete references is that, if they are subsequently
dereferenced by mistake, the program will immediately fail with a NullPointerException,
rather than quietly doing the wrong thing. It is always beneficial to detect programming errors
as quickly as possible.
VERY IMPORTANT:
So when should you null out a reference? What aspect of the Stack class makes it susceptible
to memory leaks? Simply put, the Stack class manages its own memory. The storage pool
consists of the elements of the items array (the object reference cells, not the objects
themselves). The elements in the active portion of the array (as defined earlier) are allocated,
and those in the remainder of the array are free. The garbage collector has no way of knowing
this; to the garbage collector, all of the object references in the items array are equally valid.
Only the programmer knows that the inactive portion of the array is unimportant. The
programmer effectively communicates this fact to the garbage collector by manually nulling
out array elements as soon as they become part of the inactive portion.
 
Upvote 0
I'm sorry, but when you start off a source that uses listed caches and per-request database loading, you have few chances of optimizing performance or speed. Odin needs a whole new code to run fresh. Seriously, if you're a rather experienced programmer, read some of its methods, and you'll instantly notice blatant overuses of memory.
 
Upvote 0
I'm sorry, but when you start off a source that uses listed caches and per-request database loading, you have few chances of optimizing performance or speed. Odin needs a whole new code to run fresh. Seriously, if you're a rather experienced programmer, read some of its methods, and you'll instantly notice blatant overuses of memory.

For those who suck with Threaded Programming and Applications:
To avoid the risk of deadlock, never cede control to the client within a synchronized
method or block.
In other words, inside a synchronized region, do not invoke a public or
protected method that is designed to be overridden. (Such methods are typically abstract, but
occasionally they have a concrete default implementation.) From the perspective of the class
containing the synchronized region, such a method is alien. The class has no knowledge of
what the method does and no control over it. A client could provide an implementation of an
alien method that creates another thread that calls back into the class. The newly created
thread might then try to acquire the same lock held by the original thread, which would cause
the newly created thread to block. If the method that created the thread waits for the thread to
finish, deadlock results.
 
Upvote 0
1.* Avoid using synchronization in read-only or single-threaded queries.
2.* Iterator.hasNext() and Enumerator.hasMoreElements() do not need to be repeatedly called when the size of the collection is known. Use collection.size() and a loop counter instead.
3.* Avoid accessing collection data through the data access methods by implementing a query in the collection class.
4.* Elminate repeated casts by casting once and holding the cast item in a correctly typed variable.
5.* Reimplement the collection class to specialize for the data being held in the collection.
6.* Reimplment the Map class to use a hash function which is more efficient for the data being mapped.


7.# Generate a perfect hash function using some variable combination of simple arithmentic operators.
8.# Perfect hash functions may require excessive amounts of memory.
9.# Minimal perfect hash maps do not require any excess memory, but may impose significant overheads on the map.
 
Last edited:
Upvote 0
String.equals() is expensive if you are only testing for an empty string. It is quicker to test if the length of the string is 0. If i'm not mistaken.. :o
 
Last edited by a moderator:
Upvote 0
Do not rely on finalizers to execute time critical code. Avoid using finalizers since there is no gaurantee that finalizers will be executed. If a finalizer is not executed, several finalizers from different threads can be waiting to completely execute. If this happens, its a great way to have your program come to a grinding halt.
An example of a finalizer:
Code:
try {
// code that throws exceptions
} finally {
// do what must be done here.
}

Also if an exception is thrown and not caught, the object can be corrupted which will return or merely do the wrong thing. This is also a fatal problem for your programs. Therefore avoid finalizers.
 
Upvote 0
Back