Optimizing Elasticsearch on Windows and XAMPP for Magento 2.4 (With Illustrated Guide)

When developing Magento 2.4 on a localhost environment with XAMPP, Elasticsearch can consume a significant amount of memory because it runs on Java. This often leads to slower system performance. In this guide, I'll show you how to optimize Elasticsearch and run it in the background as a Windows Service.

1. Reduce Memory Usage

Open the following file:

d:\xampp\htdocs\elasticsearch-7.10.1\config\jvm.options

Find these lines:

-Xms1g
-Xmx1g

Replace them with:

-Xms512m
-Xmx512m

A 512MB heap size is usually sufficient for Magento development environments with a moderate product catalog.

2. Install and Run Elasticsearch as a Windows Service

This allows Elasticsearch to run in the background without keeping a Command Prompt window open

Step 2.1: Open Command Prompt as Administrator

Start Menu -> Type "cmd" -> Right-Click -> Run as Admin

Step 2.2: Navigate to the Elasticsearch Bin Directory

Assuming Elasticsearch is installed in:

d:\xampp\htdocs\elasticsearch-7.10.1\bin

Run the following command:

cd d:\xampp\htdocs\elasticsearch-7.10.1\bin

Example:

Linh@LINH-PC d:\> cd d:\xampp\htdocs\elasticsearch-7.10.1\bin
Linh@LINH-PC bin>

Step 2.3: Install Elasticsearch as a Windows Service

elasticsearch-service.bat install

 This command installs Elasticsearch as a Windows Service, allowing it to run in the background without requiring an open Command Prompt window.

Step 2.4: Start / Stop the Service

Start the service:

elasticsearch-service.bat start

Stop the service:

elasticsearch-service.bat stop
NOI DUNG ANH

3. Verify Elasticsearch Is Running

Open your browser and visit:

http://localhost:9200

If you see a JSON response containing Elasticsearch information, the service is running correctly.