3

I have two functions below one is insert() and other one is startGC().

I will call insert() method first which will take some 300MB of heap space. After that I will call startGC() which should release the memory allocated in heap because all the vector objects are local to the function but its not happening.

private void insert()
 {
         Vector v=new Vector();
         Vector v1=new Vector();
         Vector v2=new Vector();
        String str="Hello";

        for (long i = 0L; i < 999999L; i++) {
            v.add(str + i);
            v1.add(str + i);
            v2.add(str + i);
        }
        v=null;
        v1=null;
        v2=null;
 }

private void startGC()
{
    System.gc();
}

My Question:

1) Why Garbage collect is not working in this example.

2) How to make JVM to garbage collect all unused memory blocks.

Any code sample to achieve the same.

Vasant
  • 301
  • 4
  • 14

6 Answers6

7
  1. In what way isn't it working?
  2. You can't tell the JVM to start a Garbage Collection, you can only suggest that you think it is a good idea by invoking System.gc().

From the JavaDoc:

Calling the gc method suggests that the Java Virtual Machine expend 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 Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Keppil
  • 45,603
  • 8
  • 97
  • 119
2

If I try

private static void insert() {
    List<String> v1 = new ArrayList<>(), v2 = new ArrayList<>(), v3 = new ArrayList<>();
    String str = "Hello";

    for (long i = 0L; i <= 999999; i++) {
        v1.add(str + i);
        v2.add(str + i);
        v3.add(str + i);
    }
}

private static void startGC() {
    System.gc();
}

private static long mbUsed() {
    return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/1024/1024;
}

public static void main(String... args)  {
    startGC();
    System.out.printf("Memory used before insert %,d MB%n", mbUsed());
    insert();
    System.out.printf("Memory used after insert %,d MB%n", mbUsed());
    startGC();
    System.out.printf("Memory used after GC %,d MB%n", mbUsed());
}

it prints

Memory used before insert 2 MB
Memory used after insert 265 MB
Memory used after GC 5 MB

So it appears to work for me.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

As already said, you can only suggest the GC to delete your objects. The only thing you might do is setting the heapmax (for example: -Xmx512m). You can affect the run of the gc this way. This is probably one of the biggest disadvantages of Java. If you really need the heap, your have to use languages like cpp, which won´t use a vm to compile the application.

Christian Lendel
  • 410
  • 1
  • 4
  • 13
1

This is a good read on Java Garbage Collection.

Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
1

You can't force a collection and it is the wrong approach. You should read up on the different, garbage-collection parameters that you can pass to your JVM and pick ones that will maximise the performance you are interested in (overall throughput? minimum pauses? etc). The code itself should not really worry about gc.

matt freake
  • 4,877
  • 4
  • 27
  • 56
1

The JVM is recycling the memory it has been allocated.

It will not necessary give it back to the OS.

Please have a look this post Java still uses system memory after deallocation of objects and garbage collection that explains how it works.

Community
  • 1
  • 1
poussma
  • 7,033
  • 3
  • 43
  • 68