In ordinary work, when measuring the performance of the server, several indicators are often involved, such as LOAD, CPU, MEM, QPS, RT and so on. Each indicator has its own unique significance. In many cases, when problems occur online, some indicators are often accompanied by abnormalities. In most cases, some indicator will show an exception in advance of the problem.

In the first article, we introduced an important indicator is Load. We mentioned that Linux has a high Load, which is mainly composed of CPU usage, memory usage and IO consumption. Too much of either can cause server load to climb dramatically. This article, the third in a series, looks at the second of several factors that affect machine load, memory usage.


Memory is one of the most important parts in the computer. It is the bridge of communication with CPU. All programs in a computer are run in memory, so the performance of memory has a great impact on the computer.

Memory, also known as internal Memory, is used to temporarily store computational data in the CPU and data exchanged with external Memory such as hard disk.

Physical memory

Physical memory refers to the memory space obtained through physical memory modules. Random Access Memory (RAM) is an internal memory that directly exchanges data with the CPU, also known as main memory (memory).

Virtual memory

Virtual memory is a technology of memory management in computer system. It makes the application that it has a continuous available memory (a continuous complete address space), and, in fact, it is usually divided into multiple physical memory fragments, some temporary storage on the external disk storage, when the need for data exchange (that is, when physical memory is insufficient, may use hard disk space to serve as memory usage). Systems that use virtual memory make it easier to write large programs and use real physical memory, such as RAM, more efficiently than systems that do not use virtual memory.

Swap partition

Swap When the physical memory of the system is insufficient, a portion of the hard disk space can be freed for use by currently running programs. The freed space may come from programs that have not done anything for a long time. The freed space is temporarily stored in Swap partition. When the programs are ready to run, the saved data is restored from Swap partition to memory.

Data loading, thread concurrency,I/O buffering, and so on at runtime depend on memory. The amount of available memory determines whether the program can run properly and how well it performs.


On a Linux machine, there are multiple commands to view the machine’s memory information. These include free, top, etc.

The free command

The free command displays free, used physical memory, swap partition, and kernel buffer memory in Linux. The free command is one of the most frequently used Linux system monitoring tools.

$free             total       used       free     shared    buffers     cachedMem:       8388608    2926968    5461640          0          0    1654392-/+ buffers/cache:    1272576    7116032Swap:     16777208          0   16777208Copy the code

In the figure above, there are three rows and six columns of data. The Mem row is the memory usage. The -/+ buffers/cache line is the cache statistics for physical memory. The Swap line is the Swap space usage.

Physical memory and Swap partitions were introduced. Here’s a bit more about buffers and cache.

Buffer and cache

A buffer is something that has yet to be “written” to disk.

A cache is something that has been “read” from the disk and stored for later use.

To put it simply:

Buffers buffers are used to store data that needs to be output to a disk to improve IO performance (memory -> disk).

Cached is used to store data read from disk, often cached to reduce IO (disk -> memory)

Buffer and cache, both are data in RAM. In simple terms, buffers are to be written to disk, and cache is to be read from disk.

After introducing the differences between buffer and cache, let’s analyze the data queried by the free command.

Mem line

             total       used       free     shared    buffers     cachedMem:       8388608    2926968    5461640          0          0    1654392Copy the code

This line shows the overall picture of physical memory.

Total: 8388608. Represents the total size of physical memory.

2:2926968. Represents the total amount allocated for cache (including buffers and cache) use, but some of the cache may not actually be used.

Free: 5461640. Represents unallocated memory.

Shared: 0. Common systems do not use shared memory.

Buffers: 0. The number of buffers allocated by the system but not used.

Cached: 1654392. Number of caches allocated by the system but not used.

Total (Mem) = Used (Mem) + Free (Mem)

– / + buffers/cache line

             total       used       free     shared    buffers     cached-/+ buffers/cache:    1272576    7116032Copy the code

2:1272576. Denotes the total amount of buffers and cache actually used, and also the total amount of memory actually used.

Free: 7116032. The sum of unused Buffers, cache and unallocated memory is the actual available memory of the system.

Used (-/+ buffers/cache) = used (Mem) -cached (Mem) -buffers (Mem)

Free (-/+ buffers/cache) = Free (Mem) + cached (Mem) + buffers (Mem)

Swap lines

$free             total       used       free     shared    buffers     cachedSwap:     16777208          0   16777208Copy the code

Total: 16777208. Swap Total memory size.

2:0. Indicates the allocated Swap size.

Free: 16777208. Represents unallocated memory.

Next, take a look at the data as a whole.

$free             total       used       free     shared    buffers     cachedMem:       8388608    2926968    5461640          0          0    1654392-/+ buffers/cache:    1272576    7116032Swap:     16777208          0   16777208Copy the code

Actual available memory size on the machine:

Free(-/+ buffers/cache) = Free(Mem)+buffers(Mem)+Cached(Mem); 7116032 = 5461640 + 0+ 1654392Copy the code

Allocated memory size:

   Used(Mem) = Used(-/+ buffers/cache)+ buffers(Mem) + Cached(Mem)          2926968 = 1272576 + 0 + 1654392Copy the code

Total physical memory size

Total (Mem) = Used (-/+ buffers/cache) + Free (-/+ buffers/cache) 8388608 = 1272576 + 7116032Copy the code

To sum up, the total memory size of the entire machine is 8388608, of which 2926968 memory has been allocated, and 5461640 memory has not been allocated. Of the 2,926,968 allocated, 1,654,392 have not been used and 1,272,576 have been used. The current machine still has 7116032 memory available.

Free Command parameters

-m Displays memory in m units

$free -m             total       used       free     shared    buffers     cachedMem:          8192       2802       5389          0          0       1559-/+ buffers/cache:       1243       6948Swap:        16383          0      16383Copy the code

-g Displays memory in g

$free -g             total       used       free     shared    buffers     cachedMem:          8          2          5         0         0           1-/+ buffers/cache:       1          6Swap:        16          0          16Copy the code

-s 2 Continuously monitors the memory status and prints the information every 2 seconds

$free -s 2         total       used       free     shared    buffers     cachedMem:       8388608    2873128    5515480          0          0    1600588-/+ buffers/cache:    1272540    7116068Swap:     16777208          0   16777208             total       used       free     shared    buffers     cachedMem:       8388608    2873168    5515440          0          0    1600628-/+ buffers/cache:    1272540    7116068Swap:     16777208          0   16777208Copy the code

In addition to free, you can also use the /proc/meminfo file on Linux to check the memory usage of the operating system. In fact, the content of the free command is also from the /proc/meminfo file.

The top command

The top command is a commonly used performance analysis tool in Linux. It displays the resource usage of each process in the system in real time, similar to the Task manager in Windows.

Using the top command to view Load Avg and CPU utilization was described in the previous two articles. One of the other things that top prints is memory.

Top-17:49:32 Up 2 days, 6:25, 1 User, load Average: 0.01, 0.09, 0.12Tasks: 30 total, 1 running, 29 sleeping, 0 stopped, 0 zombieCpu(s): Sy us 0.1%, 0.0%, 0.0% ni, 88.0% id, wa, 3.8%, 0.0% hi 0.0% si, 8.1% stMem: 8388608k total, 2884716k used, 5503892k free, 0k buffersSwap: 16777208k total, 0k used, 16777208k free, 1612080k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 85690 admin 20 0 5138M 1.1g 47m S 2.3 13.9 93:28.92 JavaCopy the code

The Mem and Swap lines above show memory usage. It also shows the memory usage of different processes. Very useful.


The JVM runs as a Process on Linux, and to Linux, the JVM is nothing more than a good kid with self-managed memory.

JVM parameters can be used to set the size of JVM memory during application startup. If this limit is exceeded, an exception is thrown. As a result, various OutofMemoryErrors are thrown, most notably in the case of high memory usage.

One situation where direct memory, that is, Linux’s physical memory, can become too high is the use of NIO. NIO introduces a channel and buffer based IO approach, which can use Native libraries to allocate out-of-heap memory directly and then operate with a DirectByteBuffer object stored in the Java heap as a reference to that memory.

Therefore, when using NIO, be careful not to cause the machine memory to overflow.

There are a number of possible reasons for the high memory footprint in the JVM. The most common is memory leaks.

Troubleshooting for memory leaks

1. Run the top command to view the IDS of processes that occupy large memory resources.

➜  ~ topPID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND3331 admin     20   0 7127m 2.6g  38m S 10.7 90.6  10:20.26 javaCopy the code

The process whose PID is 3331 occupies 90.6% of the memory. And it is a Java process, the basic conclusion is a program problem.

2. Use JMAP to check the memory status and analyze whether there is a memory leak.

Jmap-histo 3331: View the number and size of objects in the heap memory (histogram). Jmap-histo :live 3331: Jmap-dump :format=b,file=heapDump 3331: Outputs memory usage details to a fileCopy the code

Once you have the heap dump file, you can perform object analysis. If a large number of objects are continually referenced and not released, then there is a memory leak and the code needs to be combined to release unused objects.