🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Runtime freeMemory() method in Java programming and how to use it with an example.
1. Runtime freeMemory() Method Overview
Definition:
The Runtime.freeMemory() method returns the amount of free memory in the Java Virtual Machine (JVM). This represents the amount of memory that is currently unallocated and can be used by new objects created in the application.
Syntax:
public long freeMemory()
Parameters:
- None.
Key Points:
- The returned value represents the current amount of free memory, in bytes.
- This method can be useful in performance monitoring to gauge how much memory an application is consuming over time.
- Remember that the value is not constant and can change as objects are allocated and garbage collected.
- Calling System.gc() can potentially reclaim unused memory, but it's generally not recommended to force garbage collection in a typical application since it can cause performance hitches.
2. Runtime freeMemory() Method Example
public class MemoryCheck {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
long initialFreeMemory = runtime.freeMemory();
System.out.println("Initial free memory: " + initialFreeMemory + " bytes");
// Allocate large number of integers
int[] array = new int[1_000_000];
long afterAllocation = runtime.freeMemory();
System.out.println("Free memory after allocation: " + afterAllocation + " bytes");
long consumedMemory = initialFreeMemory - afterAllocation;
System.out.println("Memory consumed by allocation: " + consumedMemory + " bytes");
}
}
Output:
Initial free memory: 12345678 bytes Free memory after allocation: 11789456 bytes Memory consumed by allocation: 56222 bytes (Note: The output values are hypothetical and will differ based on your JVM's configuration and current state.)
Explanation:
In the provided example, we first determine the initial free memory using the freeMemory() method.
We then allocate an array of integers, consuming a portion of the JVM's available memory. By checking the free memory again after the allocation, we can deduce how much memory was consumed by our array allocation.
This demonstrates how you can monitor memory usage and see the impact of various operations on the available memory in the JVM.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment