1

Is there any way to utilize maximum memory of the system using strings? I'm using runtime to display the free memory. I've tried with this code:

class Mem{
       public static void main(String[] args) {
              System.out.println(Runtime.getRuntime().maxMemory());
              System.out.println(Runtime.getRuntime().totalMemory());
              System.out.println(Runtime.getRuntime().freeMemory());
              String str=new String("Hi");
              for(long i=0;i<1000000;i++){
                     str+="aa";
                     //System.out.println(i);
              }      
              System.out.println(Runtime.getRuntime().freeMemory());
       }
}

However, the garbage collector comes into action for every few iterations and frees up the memory. Is is possible to make it utilize maximum memory and display the free memory before gc frees it up?

4 Answers 4

4

A way to allocate and hold a large amount of memory quickly is to do

List<byte[]> bytes = new ArrayList<>();
for(int i = 0; i < 1000; i++)
    bytes.add(new byte[10000000]); // 10 MB
Sign up to request clarification or add additional context in comments.

4 Comments

Hehe, nice way to waste resources in a fragmented way, +1 from me.
you could catch the firsts OutOfMemory and reduce the size of the array geometrically to scratch the last bits ...
There arrays will go straight into tenured space continuously. i.e. there shouldn't be much fragmentation.
I see, then could a linked list be a better option ?
3

At the end, your string only contains 2 million characters. If you want to make your code run out of memory, change

        str += "aa";

to

        str += str;

That'll make the string grow exponentially, and no amount of garbage collection will help even with a modest number of iterations.

2 Comments

That should go faster indeed!
yeah, of course!! str+=str did it.why didn't i think of that? But its hell lot slower than I thought. I included the try/catch block as suggested by assylias and caught the OutOfMemoryException even for as few iterations as 50!!
3

str+="aa"; creates a new string each time and reassigns str so the old string is eligible to garbage collection.

However it will run out of memory at some stage if you iterate enough.

You should put the for loop in a try/catch block, catch OutOfMemoryError and include your print statement in the catch block.

Comments

3

try something like

    System.out.println(Runtime.getRuntime().maxMemory());
    System.out.println(Runtime.getRuntime().totalMemory());
    System.out.println(Runtime.getRuntime().freeMemory());
    String str = new String(new char[32_000_000]);
    System.out.println(Runtime.getRuntime().totalMemory());
    System.out.println(Runtime.getRuntime().freeMemory());

note that str += "aa" is so slow that you may make wrong conclusions. GC cannot free no memory in your case

Comments

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.