How to find max memory, free memory and total memory in Java? Last Updated : 07 May, 2021 Comments Improve Suggest changes Like Article Like Report Although Java provides automatic garbage collection, sometimes you will want to know how large the object heap is and how much of it is left. This information can be used to check the efficiency of code and to check approximately how many more objects of a certain type can be instantiated. To obtain these values, we use the totalMemory() and freeMemory methods. As we know Java's garbage collector runs periodically to recycle unused objects. We can call garbage collector on demand by calling the gc() method. A good thing to try is to call gc() and then call freeMemory(). Methods: void gc(): Runs the garbage collector. Calling this method suggests that the Java virtual machine expand effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects. Syntax: public void gc() Returns: NA. Exception: NA.long freeMemory(): This method returns the amount of free memory in the Java Virtual Machine. Calling the gc method may result in increasing the value returned by freeMemory.Syntax: public long freeMemory() Returns: an approximation to the total amount of memory currently available for future allocated objects, measured in bytes. Exception: NA.long totalMemory(): This method returns the total amount of memory in the Java virtual machine. The value returned by this method may vary over time, depending on the host environment. Syntax: public long totalMemory() Returns: the total amount of memory currently available for current and future objects, measured in bytes. Exception: NA. Java // Java code illustrating gc(), freeMemory() // and totalMemory() methods class memoryDemo { public static void main(String arg[]) { Runtime gfg = Runtime.getRuntime(); long memory1, memory2; Integer integer[] = new Integer[1000]; // checking the total memory System.out.println("Total memory is: " + gfg.totalMemory()); // checking free memory memory1 = gfg.freeMemory(); System.out.println("Initial free memory: " + memory1); // calling the garbage collector on demand gfg.gc(); memory1 = gfg.freeMemory(); System.out.println("Free memory after garbage " + "collection: " + memory1); // allocating integers for (int i = 0; i < 1000; i++) integer[i] = new Integer(i); memory2 = gfg.freeMemory(); System.out.println("Free memory after allocation: " + memory2); System.out.println("Memory used by allocation: " + (memory1 - memory2)); // discard integers for (int i = 0; i < 1000; i++) integer[i] = null; gfg.gc(); memory2 = gfg.freeMemory(); System.out.println("Free memory after " + "collecting discarded Integers: " + memory2); } } Output: Total memory is: 128974848 Initial free memory: 126929976 Free memory after garbage collection: 128632384 Free memory after allocation: 127950744 Memory used by allocation: 681640 Free memory after collecting discarded Integers: 128643696 Comment More infoAdvertise with us Next Article How are Java Objects Stored in Memory? A Abhishek Verma 16 Improve Article Tags : Java Practice Tags : Java Similar Reads How are Java Objects Stored in Memory? In Java, memory management is handled by the Java Virtual Machine (JVM). The breakdown of how objects are stored in memory:All Java objects are dynamically stored in the heap memory.References to these objects are stored in the stack memory.Objects are created using the "new" keyword and are allocat 5 min read How are Java Objects Stored in Memory? In Java, memory management is handled by the Java Virtual Machine (JVM). The breakdown of how objects are stored in memory:All Java objects are dynamically stored in the heap memory.References to these objects are stored in the stack memory.Objects are created using the "new" keyword and are allocat 5 min read How to Find Length or Size of an Array in Java? Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.In this article, we will learn:How to 3 min read How to Find Length or Size of an Array in Java? Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.In this article, we will learn:How to 3 min read Demystifying Memory Management in Modern Java Versions Memory management is the backbone of Java programming, determining how efficiently your applications use system resources. In this comprehensive guide, we will explore the intricacies of memory management in modern Java versions, including Java 8, 11, and beyond. By the end of this article, you'll h 6 min read Heap and Stack Memory Errors in Java Memory allocation in java is managed by Java virtual machine in Java. It divides the memory into stack and heap memory which is as shown below in the below media as follows: Stack memory in Java It is the temporary memory allocation where local variables, reference variables are allocated memory wh 5 min read Like