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

Example

Linh@LINH-PC d:\> cd d:\xampp\htdocs\elasticsearch-7.10.1\bin
Linh@LINH-PC bin> elasticsearch-service.bat start

Start Elasticsearch (Windows .bat file)

Create a file named: Start ElasticSearch.bat

@echo off
title Start Elasticsearch
echo Starting Elasticsearch...
cd /d "d:\xampp\htdocs\elasticsearch-7.10.1\bin"
elasticsearch-service.bat start
echo.
echo ==========================================
echo Elasticsearch started successfully!
echo ==========================================
pause

Stop Elasticsearch (Windows .bat file)

Create a file named: Stop ElasticSearch.bat

@echo off
title Stop Elasticsearchecho Stopping Elasticsearch...
cd /d "d:\xampp\htdocs\elasticsearch-7.10.1\bin"
elasticsearch-service.bat stop
echo.
echo ==========================================
echo Elasticsearch stopped successfully!
echo ==========================================
pause

NOTE
Run the .bat file as Administrator
Make sure Elasticsearch is installed as a Windows service first

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.

{
"name" : "win-node-1",
"cluster_name" : "magento2-windows-local",
"cluster_uuid" : "YPvQwevbR3Sp77OUO6cwag",
"version" : {
"number" : "7.10.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa",
"build_date" : "2020-12-05T01:00:33.671820Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}