Fw: [JavaSpecialists] Issue 173 - Java Memory Puzzle

11 views
Skip to first unread message

Victor Munoz

unread,
May 29, 2009, 8:12:53 PM5/29/09
to java...@googlegroups.com


----- Forwarded Message ----
From: Dr Heinz M. Kabutz <he...@javaspecialists.eu>
To: Victor Munoz <vmunoz...@yahoo.com>
Sent: Thursday, May 28, 2009 7:37:09 AM
Subject: [JavaSpecialists] Issue 173 - Java Memory Puzzle


Welcome to the 173rd issue of The Java(tm) Specialists' Newsletter, sent to you from Zürich in Switzerland. This is the 12th week I'm away from Crete this year alone, with another 2 weeks away before the middle of June. So out of 26 weeks in the first half of 2009, I will have been away for 14, which is more than 50%. Due to the economic crisis, I fully expected at least half of my engagements to get cancelled, but that did not happen. Our Java Specialist Master Course and Design Patterns Course are just way too popular. HOWEVER, as from the middle of June, I'll be on "holiday" in Crete for a few months, coinciding with my kids 14 week school holiday. This will give me more time to think and produce some nice newsletters. Also, I am talking to a publisher to perhaps put together a book of my most popular newsletters, so you will have something to take to the beach.

Java Memory Puzzle

Instead of telling you some mystery of Java memory, it is time for you to put on your thinking caps. I had a discussion a few weeks ago with one of my subscribers of whether you should null your local variables, to make things easier for the garbage collector.. His understanding was that the local variables will be stored on the stack and thus popped off at the end of the method call anyway, so nulling them was a waste of time. In almost all situations, he is right. However, he had a class that did something most peculiar, something like this:

public class JavaMemoryPuzzle {
  private final int dataSize =
      (int) (Runtime.getRuntime().maxMemory() * 0.6);

  public void f() {
    {
      byte[] data = new byte[dataSize];
    }

    byte[] data2 = new byte[dataSize];
  }

  public static void main(String[] args) {
    JavaMemoryPuzzle jmp = new JavaMemoryPuzzle();
    jmp.f();
  }
}
  

When you run this you will always get an OutOfMemoryError, even though the local variable data is no longer visible outside of the code block.

So here comes the puzzle, that I'd like you to ponder about a bit. If you very politely ask the VM to release memory, then you don't get an OutOfMemoryError:

public class JavaMemoryPuzzlePolite {
  private final int dataSize =
      (int) (Runtime.getRuntime()..maxMemory() * 0.6);

  public void f() {
    {
      byte[] data = new byte[dataSize];
    }

    for(int i=0; i<10; i++) {
      System.out.println("Please be so kind and release memory");
    }
    byte[] data2 = new byte[dataSize];
  }

  public static void main(String[] args) {
    JavaMemoryPuzzlePolite jmp = new JavaMemoryPuzzlePolite();
    jmp.f();
    System.out.println("No OutOfMemoryError");
  }
}
  

Why does this work? (I do know the answer, but would like to see how many of my readers can figure it out.) You can either respond via email if you are reading the newsletter via my mailing list or fill in my enquiry page if you are reading it online.

Happy pondering and with kind regards from Zürich

Heinz


Upcoming Events...

We offer all of our courses onsite at your company. More information ...

We have developed a new course to hone your skills as a Java Specialist. Have a look at our new Java Specialist Master Course. Please contact me for more information.





About Me
I have been writing for the Java specialist community since 2000. It's been fun.

It's even more fun when you share this writing with someone you feel might enjoy it. And they can get it fresh each month if they head for www.javaspecialists.eu and add themselves to the list.

Copyright Heinz Kabutz 2009
Aristotelous 84, Korakies, Akrotiri, Chania, Crete, 73100, Greece
---
You are currently subscribed to thejavaspecialists as: vmunoz...@yahoo.com
To unsubscribe send a blank email to leave-thejavaspec...@burst.sparklist.com

Reply all
Reply to author
Forward
0 new messages