🎓 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 totalMemory() method in Java programming and how to use it with an example.
1. Runtime totalMemory() Method Overview
Definition:
The Runtime.totalMemory() method returns the total amount of memory currently available for current and future objects, measured in bytes, within the JVM.
Syntax:
public long totalMemory()
Parameters:
- None.
Key Points:
- The returned value represents the current total memory allocated to the JVM, not the maximum possible memory.
- The total memory consists of used and unused memory.
- While Runtime.freeMemory() gives you the amount of memory not currently allocated, Runtime.totalMemory() minus Runtime.freeMemory() gives you the actual memory currently in use by your application.
- The JVM might increase the allocated memory pool over time, up to the maximum memory limit, based on application demands.
2. Runtime totalMemory() Method Example
public class TotalMemoryCheck {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
long initialTotalMemory = runtime.totalMemory();
System.out.println("Initial total memory: " + initialTotalMemory + " bytes");
// Allocate large number of integers
int[] array = new int[1_000_000];
long afterAllocation = runtime.totalMemory();
System.out.println("Total memory after allocation: " + afterAllocation + " bytes");
long differenceInMemory = afterAllocation - initialTotalMemory;
System.out.println("Difference in memory after allocation: " + differenceInMemory + " bytes");
}
}
Output:
Initial total memory: 25794976 bytes Total memory after allocation: 25794976 bytes Difference in memory after allocation: 0 bytes (Note: The output values are hypothetical and may differ based on your JVM's configuration and current state. In some cases, you may see a difference in total memory after the allocation, especially if the JVM decides to increase its memory pool.)
Explanation:
In the provided example, we first determine the initial total memory using the totalMemory() method.
After that, we allocate an array of integers. We then measure the total memory again. The difference in memory before and after allocation provides an indication of how the JVM's memory pool is being utilized or if it has been expanded.
This method helps in understanding how memory is managed by the JVM and can be used in conjunction with other Runtime methods for a more comprehensive view of the application's memory utilization.
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