Your Search Bar For Shrewd Tips

How Do I Clear Jvm Cache?


If an application is slow, showing outdated data, or behaving unexpectedly, you may be wondering how to clear the JVM cache. The important thing to know is that there is no single cache-clearing command for every Java application. The Java Virtual Machine (JVM) manages several kinds of temporary data, while applications, web browsers, build tools, and application servers may maintain separate caches of their own.

In many cases, restarting the Java process is the simplest way to clear temporary, in-memory JVM data. For other problems, you need to identify which cache is involved and use the right tool. This guide explains what “JVM cache” can mean, how to clear common Java caches safely, and what to check if the issue returns.

What Does “JVM Cache” Mean?

The JVM does not keep one universal cache that you can empty with a standard command. Instead, a running Java process may use different kinds of temporary or optimized data. The steps to clear them depend on where that data is stored and which component created it.

  • JIT code cache: The Just-In-Time compiler stores compiled machine code in memory to help frequently used Java code run faster.
  • Class metadata and loaded classes: The JVM keeps information about classes loaded by the application. This data generally remains associated with the running process or class loader.
  • DNS cache: Java may temporarily remember the IP address returned for a host name.
  • Application cache: Frameworks and applications can cache database results, templates, configuration, or other data.
  • Java deployment cache: Older Java desktop technologies could store downloaded application files locally.
  • Build-tool cache: Maven and Gradle store dependencies and build outputs, but these are not JVM caches.

Before clearing anything, identify what is stale or causing the problem. Deleting unrelated files can waste time, force large downloads, or disrupt a working application.

The Quickest Way to Clear Temporary JVM Data

For most ordinary Java applications, stop and restart the application. Exiting the process releases its in-memory JIT code, loaded classes, and other process-local data. Starting it again creates a fresh JVM and allows the application to rebuild what it needs.

  1. Save any work handled by the application.
  2. Stop the application using its normal shutdown option or service controls.
  3. Confirm that the Java process has exited.
  4. Start the application again and check whether the problem is resolved.

On Linux or macOS, you can inspect Java processes with jps -l, if the JDK tools are installed. On Windows, use Task Manager or an administrator-approved process monitoring tool. Avoid terminating every process named java unless you know what each one does: another Java process could be a server, scheduled job, or service that other users depend on.

Restarting is usually preferable to trying to manipulate JVM memory directly. The JVM is designed to manage its own runtime data, and there is no general-purpose command that safely empties every in-memory cache while an application continues running.

How to Clear the Java DNS Cache

If a Java program continues connecting to an old server address after a DNS change, the issue may be Java’s DNS lookup cache. Restarting the process clears its cached lookup results. In some applications, you can also adjust the JVM’s DNS cache policy, but this setting affects behavior and should be chosen carefully.

Java security properties commonly used for DNS caching include networkaddress.cache.ttl, which controls how long successful lookups are retained, and networkaddress.cache.negative.ttl, which controls how long failed lookups are retained. These are Java security properties, not ordinary application properties, and the correct configuration method can vary by Java version and deployment.

For a service that needs updated DNS records quickly, consult the documentation for its Java version and hosting environment before changing the policy. Very short cache times may increase DNS traffic, while long cache times can delay recognition of address changes. Also verify that the operating system, DNS resolver, or network infrastructure is not caching the old result independently.

How to Clear the Java Web Start or Deployment Cache

Some users searching for “clear JVM cache” are actually trying to remove cached Java desktop application files. The legacy Java Control Panel included a way to delete temporary Internet files used by Java Web Start and applets. In versions of Java that provide the Java Control Panel, look for its temporary-file or cache settings and use the option to delete the stored files. The exact labels vary by release.

Java Web Start and browser applets were removed from standard Oracle JDK releases beginning with Java 11. If you use an older Java installation, or a compatible alternative such as OpenWebStart, follow the instructions for that specific product. Do not assume that deleting a folder named “Java” is safe: it may contain configuration or data beyond disposable downloads.

After clearing a deployment cache, the application may need to download its files again. Use this approach only when the problem relates to a Java desktop application or downloaded deployment files, not as a routine fix for a server-side Java process.

How to Handle the JVM JIT Code Cache

The JIT code cache holds machine code compiled while a Java program runs. It is managed by the JVM and normally requires no manual clearing. Restarting the Java process gives the application a fresh code cache. The JVM then compiles code again as needed, so performance may be temporarily different while it warms up.

If logs report that the code cache is full, repeatedly clearing caches is unlikely to be the right long-term solution. Check the Java version, JVM options, application workload, and monitoring data. A Java administrator may be able to adjust code-cache settings, such as the maximum code-cache size, but increasing a limit consumes more memory and should be based on measurements. Do not change production JVM flags without testing and reviewing the service’s memory limits.

Similarly, a class cannot generally be “unloaded” just because a user wants to empty a cache. Class unloading depends on factors such as class-loader reachability and garbage collection. If an application has a class-loader leak, restarting may temporarily relieve symptoms, but developers should investigate why obsolete class loaders remain reachable.

Clearing Application Caches in Java

Many Java applications have their own cache settings, separate from the JVM. Examples include Hibernate’s second-level cache, Spring caches, servlet-container caches, and custom in-memory or disk-based caches. These may store query results, session data, templates, or configuration. Restarting the JVM might clear an in-memory cache, but it will not necessarily remove a cache stored on disk or in an external system such as Redis.

Check the application’s administration console, configuration files, or documentation for a supported cache-clear operation. If the application provides a management endpoint, use its documented controls and permissions. For a production service, confirm the effect first: clearing a heavily used cache can increase database or network traffic while the application repopulates it.

  • For outdated application data, identify the specific cache and its expiration or invalidation rules.
  • For stale configuration, use the supported reload procedure or restart the service if required.
  • For a distributed cache, clear the relevant entries through the cache platform rather than deleting local JVM files.
  • For suspected memory leaks, collect diagnostics and investigate the cause instead of relying on repeated restarts.

Maven and Gradle Caches Are Not JVM Caches

If a Java project is compiling against outdated dependencies, the issue may be in Maven or Gradle’s local repository. These build caches are stored on disk and are not cleared by restarting the JVM. Remove or refresh only the dependency or build output associated with the problem where possible.

With Maven, the -U option asks Maven to check for updated snapshots and releases according to repository policies. If a specific artifact is corrupted, removing that artifact’s directory from the local Maven repository can force it to be downloaded again. Avoid deleting the entire repository unless you are prepared for a full dependency download.

With Gradle, use the project’s wrapper and its documented refresh or clean tasks. For example, ./gradlew clean removes project build outputs, while dependency refresh options can request updated dependency resolution. These commands affect the build process; they do not clear the runtime cache of a separately running Java application.

Should You Delete the JVM Shared Class Data Archive?

Some Java installations use Class Data Sharing (CDS) archives to help applications start faster or use memory more efficiently. An archive is not the same as a temporary cache that should routinely be emptied. The JVM generally manages its use, and custom archives may be tied to a particular Java version or configuration.

Do not delete files from the Java installation directory just to “clear the JVM.” If you suspect a damaged or incompatible custom archive, check the application’s startup options and the Java distribution’s documentation. Rebuild or replace a custom archive only when you understand how it was created and can test the change. For a normal application issue, restarting the process is safer.

Common Problems and Safe Troubleshooting

If restarting does not resolve the problem, gather evidence before deleting files or changing JVM options. Note the exact error message, Java version, application version, and whether the issue occurs after a configuration change, deployment, or DNS update. Logs and metrics can help distinguish a cache issue from a bug, connectivity failure, or memory shortage.

  • Stale server address: Check DNS resolution and Java DNS caching; restart the process if appropriate.
  • Old application results: Inspect the application’s own cache configuration and invalidation rules.
  • Unexpected behavior after deployment: Verify that the correct application version and configuration are running, then follow the service’s deployment procedure.
  • Slow startup after a restart: Allow for normal JVM and application warm-up before judging performance.
  • Out-of-memory errors: Review heap usage, garbage-collection logs, and memory limits. Clearing a cache may not address a leak or undersized heap.

Be especially cautious with commands that delete directories, kill processes, or remove Java installation files. Confirm the path and purpose first, and follow your organization’s backup and change-management practices for production systems.

Conclusion

To clear JVM cache, first determine what cache you mean. For temporary data held by a running Java process, a controlled restart is usually the simplest and safest option. For Java DNS results, desktop deployment files, application-managed caches, or Maven and Gradle data, use the corresponding documented controls instead. The JVM’s JIT code cache and shared class archives are normally managed automatically and should not be deleted by hand.

When a cache problem keeps returning, investigate its cause rather than clearing data repeatedly. Identifying the right cache helps you resolve stale information without disrupting unrelated applications, losing useful files, or creating avoidable performance problems.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →