This article is from the official account: Java Universe (wechat id: Javagogo)

The original link: mp.weixin.qq.com/s/rCj9FWy7-… By Lin 䭽


Before I answer “Yes or no,” I want to talk about the support for large memory pages in both languages.

Of course, the prerequisite for both languages to be able to use large memory pages is to turn on the operating system’s large memory pages, which is not the focus of this article. Once this condition is satisfied, I will talk about the configuration of the two languages.

Go

Go is a compilation and execution language.

On the front end of the Go compiler, the source code is converted to AST; On the back end of the Go compiler, the AST goes through several optimization steps and is translated into target machine code. So Go’s memory allocator basically maps directly to the OPERATING system’s API. Because Go has no virtual machines.

Go also provides a low-level library syscall, which directly supports hundreds of system calls. For details, please refer to the official documentation of Go.

The syscall.madvise system call directly tells the operating system whether programs in a certain memory range are using large memory paging to speed TLB access. This can also be referenced in madise’s documentation in Linux. The main purpose of this tool is to tell the operating system how to use a certain area of memory, and turning on large memory pages is one of its options.

The following program allocates memory via malloc, and then prompts the operating system with madvise to use large memory pages:

#include <sys/mman.h>
size_t size = 256*1024*1024;
char* mymemory = malloc(size);
madvise(mymemory, size, MADV_HUGEPAGE);
Copy the code

In the Go language, the runtime.sysAlloc and syscall.Madvise functions are used.

Java

The JVM is a virtual machine that applies just-in-time to translate virtual instructions into machine code execution during virtual instruction execution.

The JVM has a complete dynamic memory management solution of its own, and there are many memory management tools to choose from. When using the JVM, Java provides an UnSafe class to help you perform low-level operations, but normally we don’t use the UnSafe class. The UnSafe class, on the one hand, is featureless, and on the other hand, its name implies that it is dangerous, HHH.

Java has a virtual machine parameter: XX:+UseLargePages, which when turned on, the JVM starts trying to use large memory pages.

So, should you use large memory pages or not?

First, you can analyze the characteristics of your application to see if there is a need for large memory pages. Usually the OS is 4K and you don’t have to use large memory pages over and over again.

In addition, you can use perF to measure some performance indicators of your system, including ITLB-load-miss, which can be used to measure TLB miss. If you find that your system has a high TLB Miss, you can delve into whether you need to turn on large memory pages.


Java Universe (wechat id: Javagogo)