Back to blog

Lineage 2 Server Performance Optimization

Contents
  1. JVM configuration: the performance foundation
  2. MySQL database optimization
  3. Multithreading and parallelism
  4. Caching and memory optimization
  5. Monitoring and profiling
  6. Network optimization
  7. Optimization checklist
  8. Conclusion

Server performance is a key factor in a Lineage 2 project’s success. Even minor lag and freezes can spoil gameplay and drive players away. This article covers optimization methods for fast, stable operation under high player loads.

JVM configuration: the performance foundation

The Java Virtual Machine is at the heart of an L2 server. Correct JVM configuration can improve performance two to three times.

Choosing a garbage collector

For L2 servers, a suitable choice is G1GC (Garbage First Garbage Collector). It reduces pauses and handles large heaps efficiently.

java -Xms4G -Xmx4G \
  -XX:+UseG1GC \
  -XX:MaxGCPauseMillis=50 \
  -XX:G1HeapRegionSize=16M \
  -XX:+ParallelRefProcEnabled \
  -XX:+UseStringDeduplication \
  -jar L2Server.jar

Key parameters:

  • -Xms4G -Xmx4G — equal initial and maximum heap sizes prevent dynamic resizing
  • MaxGCPauseMillis=50 — a 50 ms GC pause target for game servers
  • G1HeapRegionSize=16M — region size for large objects
  • ParallelRefProcEnabled — parallel processing of weak references
  • UseStringDeduplication — string deduplication can save up to 30% of memory
Tip: For 1,000 or more concurrent players, at least 8 GB of JVM RAM is recommended. Leave 2–4 GB for the OS and MySQL instead of assigning all server memory to Java.

Additional JVM parameters

# Performance options
-XX:+AggressiveOpts
-XX:+UseFastAccessorMethods
-XX:+OptimizeStringConcat

# Monitoring and logging
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-Xloggc:gc.log

# Large pages (if supported by the operating system)
-XX:+UseLargePages

MySQL database optimization

The database often becomes a bottleneck under load. Here are approaches to optimizing it.

my.cnf configuration

[mysqld]
# Basic settings
innodb_buffer_pool_size = 4G
innodb_log_file_size = 512M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT

# Performance
max_connections = 300
thread_cache_size = 50
table_open_cache = 2000
query_cache_type = 1
query_cache_size = 256M

# InnoDB settings
innodb_thread_concurrency = 8
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_io_capacity = 2000

Parameter notes:

  • innodb_buffer_pool_size — allocate 50–70% of the server’s RAM to MySQL
  • innodb_flush_log_at_trx_commit = 2 — balance performance and reliability
  • query_cache_size — cache repeated SELECT queries
  • innodb_io_capacity — use 2,000–5,000 for SSD storage and 200 for HDDs

Indexes and query optimization

Check slow queries regularly:

# In my.cnf
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1

# Analyze slow queries
mysqldumpslow -s t -t 10 /var/log/mysql/slow.log

Important indexes for L2:

-- characters table
CREATE INDEX idx_char_name ON characters(char_name);
CREATE INDEX idx_account_name ON characters(account_name);
CREATE INDEX idx_online ON characters(online);

-- items table
CREATE INDEX idx_owner ON items(owner_id);
CREATE INDEX idx_item_id ON items(item_id);

-- clan_data table
CREATE INDEX idx_clan_level ON clan_data(clan_level);

-- Composite indexes for common queries
CREATE INDEX idx_char_online_level ON characters(online, level);
Important: Avoid excessive indexes. Each index slows down INSERT and UPDATE operations. Create indexes used by frequent queries.

Multithreading and parallelism

L2 servers are inherently multithreaded. Correct thread-pool configuration matters.

Thread pools in configuration files

# Config.java or server.properties

# General thread pool
ThreadPoolSizeGeneral = 8

# AI thread pool
ThreadPoolSizeAI = 6

# Client packet thread pool
ThreadPoolSizeClientPackets = 4

# Effect thread pool
ThreadPoolSizeEffects = 4

Suggested pool sizes:

  • Up to 500 concurrent players: General=4, AI=3, ClientPackets=2
  • 500–1,000 players: General=8, AI=6, ClientPackets=4
  • 1,000 or more: General=12, AI=8, ClientPackets=6
Tip: Keep pool sizes within twice the CPU core count. Excessive threads increase context switching and reduce performance.

Caching and memory optimization

Object caching

Cache frequently requested data:

// Cache NPC templates
private static final Map<Integer, L2NpcTemplate> npcCache = 
    new ConcurrentHashMap<>();

public L2NpcTemplate getTemplate(int npcId) {
    return npcCache.computeIfAbsent(npcId, 
        id -> NpcTable.getInstance().getTemplate(id));
}

Lazy loading

Use lazy loading instead of loading every dataset at server startup:

// Eager loading: load all quests at startup
public void loadAllQuests() {
    for (int i = 1; i <= 10000; i++) {
        loadQuest(i);
    }
}

// Lazy loading: load each quest on demand
private Map<Integer, Quest> questCache = new HashMap<>();

public Quest getQuest(int questId) {
    return questCache.computeIfAbsent(questId, 
        this::loadQuest);
}

Monitoring and profiling

You cannot optimize what you do not measure.

Monitoring tools

JConsole / VisualVM — built-in JVM monitoring tools:

# Connect remotely
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

What to monitor:

  • CPU usage by thread
  • Heap memory utilization
  • GC pause duration and frequency
  • Loaded class count
  • Active threads

Performance logging

// Measure time spent in a critical operation
long startTime = System.nanoTime();
// ... time-sensitive operation ...
long duration = System.nanoTime() - startTime;

if (duration > 50_000_000) { // Over 50 ms
    LOG.warning("Slow operation: " + duration / 1_000_000 + "ms");
}

Network optimization

TCP settings

Tune the Linux network stack:

# /etc/sysctl.conf

# Increase buffer sizes
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# Settings for high load
net.ipv4.tcp_max_syn_backlog = 8192
net.core.netdev_max_backlog = 5000
net.core.somaxconn = 1024

# Apply the settings
sysctl -p

Packet batching

Batch small packets:

// Send packets in batches
private final List<L2GameServerPacket> batchQueue = new ArrayList<>();

public void sendPacket(L2GameServerPacket packet) {
    batchQueue.add(packet);
    
    if (batchQueue.size() >= 5 || shouldFlush()) {
        flushBatch();
    }
}

Optimization checklist

Check before launch:

  • ✅ JVM configured with G1GC and an appropriate heap size
  • ✅ MySQL configured for your workload
  • ✅ Critical tables have indexes
  • ✅ Thread pools match the CPU core count
  • ✅ Monitoring and logging enabled
  • ✅ OS network parameters tuned
  • ✅ Object caching enabled
  • ✅ Database stored on SSD

Conclusion

L2 server optimization is an ongoing process. Monitor performance, identify bottlenecks and test changes regularly. Correct configuration can increase concurrent player capacity two to three times on the same hardware.

Remember: measure, optimize, verify. Test changes gradually on a test server instead of applying everything at once.

Need help with optimization? We configure and optimize L2 servers. Contact us for a consultation.