2

Is it possible to do it programmatically?

Here is what I've tried:

import java.util.ArrayList;
import java.util.List;

public class TestMemory {

    public static void main(String[] args) {

        int kb = 1024;

        //Getting the runtime reference from system
        Runtime runtime = Runtime.getRuntime();

        //Print used memory
        System.out.println("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / kb);

        //Print free memory
        System.out.println("Free Memory:" + runtime.freeMemory() / kb);

        //Print total available memory
        System.out.println("Total Memory:" + runtime.totalMemory() / kb);

        //Print Maximum available memory
        System.out.println("Max Memory:" + runtime.maxMemory() / kb);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            for (int j = 0; j < 1000; j++) {
                list.add("abcdefghij" + i + j);
            }
            if (i % 1000 == 0) {
                try {
                    Thread.sleep(500);
                    System.out.println("Free Memory:" + runtime.freeMemory() / kb + " Kb");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

and the output:

Used Memory:289
Free Memory:15582
Total Memory:15872
Max Memory:126720
Free Memory:15405 Kb
Free Memory:22820 Kb (long pause, longer than 500ms and then exception)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOfRange(Arrays.java:2694)
    at java.lang.String.<init>(String.java:203)
    at java.lang.StringBuilder.toString(StringBuilder.java:405)
    at TestMemory.main(TestMemory.java:32)

It doesn't say to much to me. I expect "Free Memory" value to decrease before the crash. How to write a cycle checking the available memory and informing me about the amount of memory before this exception?

1
  • What are you actually trying to do? Just filling up memory does not sound like a real usecase to me. Commented Nov 12, 2014 at 11:53

3 Answers 3

1

I'don't think it's a good way to try to manage your own program to avoid OutOfMemoryException. You have to size before your heap to match your need of memory. Anyway for me your code is correct but you need to call the GC : System.gc() before call runtime.freeMemory().

 if (i % 1000 == 0) {
            try {
                System.gc();
                Thread.sleep(500);
                System.out.println("Free Memory:" + runtime.freeMemory() / kb + " Kb");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for System.gc(). It has made the output more exact.
1

I've changed the line if (i % 1000 == 0) { to if (i % 100 == 0) {

And the output changed to:

Free Memory:73604 Kb
Free Memory:66637 Kb
Free Memory:54902 Kb
Free Memory:51044 Kb
Free Memory:44013 Kb
Free Memory:36982 Kb
Free Memory:27633 Kb
Free Memory:20661 Kb
Free Memory:13691 Kb
Free Memory:4352 Kb
Free Memory:54 Kb

The program just had not enough time to show the decreasing.

Comments

0

I would say add a catch statement that will catch when the heap runs out of memory. The methods you call return how much memory exists in your system, not what was allocated for heap space when running your program. The JVM allocates memory on its own, while languages like C would require calls to malloc() to do this job.

1 Comment

I haven't been able to catch this exception. The program stops immediately after it has been thrown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.