Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Java Memory Management

Java programmers know that JVM runs a Garbage Collector, and System.gc() would just be a suggestion to JVM to run a Garbage Collector. It is not necessarily that if we use System.gc(), it would immediately run the GC.

Please correct me if I misunderstand Java's Garbage Collector.

Is/are there any other way/s doing memory management other than relying on Java's Garbage Collector?
If you intend to answer the question by some sort of programming practice that would help managing the memory, please do so.

like image 497
Michael 'Maik' Ardan Avatar asked Dec 13 '12 07:12

Michael 'Maik' Ardan


People also ask

Does Java allow memory management?

Java Memory Management, with its built-in garbage collection, is one of the language's finest achievements. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse.

What is JVM and explain me the Java memory allocation?

Java Memory Structure: JVM defines various run time data area which are used during execution of a program. Some of the areas are created by the JVM whereas some are created by the threads that are used in a program. However, the memory area created by JVM is destroyed only when the JVM exits.

How many types of memory management are there in Java?

There are two kinds of memory used in Java. These are called stack memory and heap memory. Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory.

How we can improve the memory management in java8?

Specifying the memory options with the following options: Initial heap size -Xms512m – set the initial heap size to 512 megabytes. Maximum heap size -Xmx1024m – set the maximum heap size to 1024 megabytes. Thread stack size -Xss1m – set the thread stack size to 1 megabytes.

What is memory management in Java?

Memory Management in Java. In Java, memory management is the process of allocation and de-allocation of objects, called Memory management. Java does memory management automatically. Java uses an automatic memory management system called a garbage collector. Thus, we are not required to implement memory management logic in our application.

Why does the JVM use more memory than the heap?

During the garbage collection objects that are no longer used are cleared, thus making space for new objects. Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.

What is JVM memory structure in Java?

Java Memory Structure: JVM defines various run time data area which are used during execution of a program. Some of the areas are created by the JVM whereas some are created by the threads that are used in a program. However, the memory area created by JVM is destroyed only when the JVM exits.

What is the difference between method area and memory area in Java?

The memory areas are destroyed when JVM exits, whereas the data areas are destroyed when the thread exits. Method Area is a part of the heap memory which is shared among all the threads. It creates when the JVM starts up. It is used to store class structure, superclass name, interface name, and constructors.


Video Answer


2 Answers

The most important thing to remember about Java memory management is "nullify" your reference.

Only objects that are not referenced are to be garbage collected.

For example, objects in the following code is never get collected and your memory will be full just to do nothing.

List objs = new ArrayList();
for (int i = 0; i  < Integer.MAX_VALUE; i++) objs.add(new Object());

But if you don't reference those object ... you can loop as much as you like without memory problem.

List objs = new ArrayList();
for (int i = 0; i  < Integer.MAX_VALUE; i++) new Object();

So what ever you do, make sure you remove reference to object to no longer used (set reference to null or clear collection).

When the garbage collector will run is best left to JVM to decide. Well unless your program is about to start doing things that use a lot of memory and is speed critical so you may suggest JVM to run GC before going in as you may likely get the garbaged collected and extra memory to go on. Other wise, I personally see no reason to run System.gc().

Hope this helps.

like image 104
NawaMan Avatar answered Oct 18 '22 05:10

NawaMan


Below is little summary I wrote back in the days (I stole it from some blog, but I can't remember where from - so no reference, sorry)

  1. There is no manual way of doing garbage collection in Java.
  2. Java Heap is divided into three generation for the sake of garbage collection. These are the young generation, tenured or old generation, and Perm area.
  3. New objects are created in the young generation and subsequently moved to the old generation.
  4. String pool is created in Perm area of Heap, Garbage collection can occur in perm space but depends on upon JVM to JVM.
  5. Minor garbage collection is used to move an object from Eden space to Survivor 1 and Survivor 2 space, and Major collection is used to move an object from young to tenured generation.
  6. Whenever Major garbage collection occurs application, threads stops during that period which will reduce application’s performance and throughput.
  7. There are few performance improvements has been applied in garbage collection in Java 6 and we usually use JRE 1.6.20 for running our application.
  8. JVM command line options -Xms and -Xmx is used to setup starting and max size for Java Heap. The ideal ratio of this parameter is either 1:1 or 1:1.5 based on my experience, for example, you can have either both –Xmx and –Xms as 1GB or –Xms 1.2 GB and 1.8 GB.

Command line options: -Xms:<min size> -Xmx:<max size>

like image 29
aviad Avatar answered Oct 18 '22 05:10

aviad