# Performance

A server with a few dozen users needs no tuning. This page is for the others: many logins a second, very large files, hundreds of jobs, or a server that has become slow and nobody knows why.

It goes from measuring to the things that most often matter, in the order they usually matter.

## Measure first

| Look at | For |
|---|---|
| the [Dashboard](Admin-Dashboard.md) and the [Overview](Admin-Overview.md) | memory in use, threads, processor, open files, connected users, as numbers and charts. "Server memory high" appears there when the heap is nearly full. |
| [Metrics](Monitoring-Metrics.md) | the same numbers over weeks, in your own monitoring |
| [Health checks](Monitoring-Health.md) | whether the server takes logins at all |
| [Reports](Reports.md) | who transfers how much, and when the peaks are |
| a [thread dump](Ops-Thread-Dumps.md) | what the server is doing at this moment, when it is slow now |

Change one thing at a time and look at the same number again.

## Memory

The server runs in Java with a fixed most memory, the heap. When the heap is too small for the work, the server spends its time making room and gets slow long before it fails.

### How much

| Server | A starting point |
|---|---|
| small: tens of users, ordinary files | 1 GB |
| busy: hundreds of users at once, jobs through the day | 2 to 4 GB |
| large: thousands of connections, previews, many jobs, AS2 version 1 with big files | 8 GB and more |

Leave memory for the operating system: its file cache is what makes disks fast. A heap of more than three quarters of the machine's memory is too much.

### Where it is set

| Where | How |
|---|---|
| the admin pages | **Set max server memory** on the Dashboard and the Overview: "Max server memory set to … MB." It rewrites the start setting below. |
| Linux and other Unix | the first option of `JAVA_OPTS` near the top of `crushmft_init.sh`: `-Xmx512m` on a new installation. `2g`, `8g` work too. |
| Windows and macOS | the line `memory=` in the launcher's `.conf` file in the server's folder: megabytes, such as `2048`, or a share of the machine's memory, such as `50%` |

The new value counts from the next start of the server. More on both: [Java and memory settings](Java-and-Memory.md).

A new Linux installation starts with 512 MB. That is enough to try the server, and too little for most real work: raise it first.

### What uses memory

| Work | Why | To use less |
|---|---|---|
| many connections | each holds buffers | limits per user and address: [Restrictions](Prefs-Restrictions.md) |
| AS2 version 1 | holds each message in memory, several times over | AS2 version 2 for large files: [AS2 overview](AS2.md#two-engines) |
| [previews](Prefs-Preview.md) | pictures are decoded in memory | fewer preview workers, or make previews in a job at night |
| folder listings with very many items | the list is built whole | sub folders. The web interface warns: "Large directory listings cause significant performance issues." |
| downloads through the web interface's advanced mode | chunks wait in memory for the browser | the server limits these by itself, to a quarter of the heap |
| debug logging | lines wait to be written | turn debug off when you are done: [Logging](Logging.md) |

Jobs with very many files do not hold their lists in memory: [Large lists](Jobs-Large-Lists.md).

If memory grows and never falls back, even at night, take a heap dump and send it to support: [Thread dumps and what to send support](Ops-Thread-Dumps.md).

## Open files and connections

Every connection and every open file takes one "open file" from the operating system, and systems limit them, often to 1024.

On Linux the start script raises the limit itself: `MAX_OPEN_FILES=16000` near the top of `crushmft_init.sh`. If it cannot, it says so: "Warning: could not allow … open files". Raise the number for servers with thousands of connections.

A server at its limit refuses new connections while old ones go on working. The log names "Too many open files".

## Logins

A login is the costliest thing a session does: the user is looked up, the password is hashed on purpose slowly, folders are connected. A client that logs in anew for every file can load a server more than a hundred users who stay.

This has a page of its own: [Login frequency and slow logins](Security-Login-Load.md).

In short:

- look at who logs in most often, and ask them to reuse their session;
- check logins that go to a directory or an identity provider: each waits for that system. Their cache times are on the plugin: [CrushLDAPGroup](Plugin-CrushLDAPGroup.md);
- give machine accounts keys in place of passwords for SFTP: [SSH keys](Security-SSH-Keys.md).

## What version 27 does differently under load

Four things that made earlier versions wait under very many short sessions are built differently:

| Before | Now |
|---|---|
| every log line was added under one lock, so hundreds of sessions could queue to write a line | lines are added without a lock. No line is ever dropped: if the writer falls behind, lines wait in memory. |
| a logout searched the whole list of sessions by comparing them | a session is found directly |
| each session with a time limit had a thread of its own to watch the clock | one thread watches all of them |
| SSH work started new threads on the network thread, which stalled other connections for the moment | SSH work runs on a pool of workers, named `SSH-WORKER-…` in a thread dump |

Nothing has to be set for these.

## Logging

At the usual level, logging costs little. At debug level 2 a busy server writes very much, and to a slow disk that is felt.

- Keep the log on a fast local disk, not on a network share: [Log files](Logging-Files.md).
- Use debug for the time of a test, on the kinds you need: [What is logged](Prefs-Logging-Events.md).
- A log target that is slow or down, such as a syslog server over TCP or a database, should not be allowed to hold the server: see the notes on each target in [Logging](Logging.md).

## Storage

The server is as fast as the place the files are.

| Storage | Mind |
|---|---|
| a local disk | the usual limit is the disk itself. Many small files are slower than few large ones, on every system. |
| a network share (SMB, NFS) | every step is a network round trip. Listings of large folders are slow. |
| object storage (S3, Azure, Google) | uploads go in parts, in parallel: the number of threads and the part size are on the folder's settings, see [the kinds of storage](VFS.md). Listings and renames cost more than on a disk; a rename of a folder is a copy of everything in it. |
| another server (FTP, SFTP) behind a folder | twice the transfer: in, and on. The far server's speed is yours. |
| encrypted folders | encryption costs processor time on every byte |

A virus scanner of the operating system that inspects every file as it is written can halve upload speed. Exclude the server's own folders (not the users' files) from on-access scanning, and scan uploads on purpose, in a job.

## Transfer speed

| If | Look at |
|---|---|
| all transfers are slow at the same number | a limit: the server's [Bandwidth](Prefs-General-Bandwidth.md), the user's speed limits in [Restrictions](Users-Restrictions.md) |
| SFTP is slower than HTTPS for the same file | the client's buffer and window sizes, and the cipher. A modern client and AES-GCM or ChaCha20 help: [SFTP keys and ciphers](Ports-SFTP-Keys-and-Ciphers.md) |
| one far-away partner is slow | the network between you. Parallel transfers help more than any setting. |
| uploads in the browser are slow for huge files | let the web interface send in parts, in parallel: [Customizations: Upload](Users-WebInterface-Customizations.md#upload) |

## Jobs

- A job that walks a million files is slow because of the million, not because of the server. Narrow the Find: [Find task](Task-Find.md).
- Jobs run in the job engine, apart from the server that takes logins, and the engine can be on another machine: [Job engines](Jobs-Engines.md), [Engines on other machines](Jobs-Engines-Other-Machines.md).
- Many jobs on the same minute compete. Spread schedules: [Schedules](Jobs-Schedules.md).

## Background work

| Work | Runs | Tame it |
|---|---|---|
| the [search index](Search-Index.md) | when files change, and on its schedule | index only what people search |
| previews | at upload or first view | see above |
| [quota](Quota.md) counting | when asked, by walking the folder | fewer, larger folders; quotas where they are needed |
| [reports](Reports-Schedules.md) on a schedule | at their time | at night |
| [replication](Replication.md) in a cluster | at every change | a fast link between the servers |

## More than one server

When one machine is not enough, or must not be the only one: [High availability](High-Availability.md). A cluster shares logins across servers; a load balancer in front spreads them.

## A slow server: where to look, in order

1. **Is it the server?** Processor and memory on the Dashboard. If both are calm, look at the network and the storage.
2. **Memory nearly full?** Raise the heap, restart at a quiet time, and watch whether it fills again.
3. **Processor full?** Take three thread dumps, ten seconds apart. What most threads are doing is the answer: [Thread dumps](Ops-Thread-Dumps.md).
4. **Many logins?** [Login frequency and slow logins](Security-Login-Load.md).
5. **One user, one folder, one job?** [Reports](Reports.md) and the [log](Admin-Log.md) say who is busy.
6. **Since when?** What changed that day: a new partner, a new job, an update, a new virus scanner.

## Not tried for this page

No load was put on a server for this page. The four changes under load are described from their design and build record, which says what was tested: a stress harness on the new parts, and a run on a test server of the earlier version. The starting points for memory are rules of thumb, not measurements.
