The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Re: look ! What's wrong with java in memory ?

Posted by Wil on August 26, 2001 at 9:01 PM

> try the following program, it will throw
> a out of memory exception when it get about
> no more than 10M memory. ( it require 32 M )
> what's wrong with java!!!

> class MType {
> int dummy;
> }
> public class MemTest {
> public static void main(String[] args) {
> int maxmsg = 8000000;
> MType[] msg = new MType[maxmsg];
> for(int i= 0; i< maxmsg; i++) {
> if( i % 1000 == 0)
> System.out.println(i);
> msg[i] = new MType();
> }
>
> }
> }
>

It is not 32MB. Objects require memory, not just the data inside I believe.

> MType[] msg = new MType[maxmsg];
This uses memory too?

Keep track of the memory free and used with Runtime.
When I tested it, it took roughly about 16 bytes (it differs) for each object.
so 16 * 8,000,000 = 128MB.

At the end of the Out of memory exception, Java used roughly about half (64M) of the total memory on my system.

Runtime rt = Runtime.getRuntime();

After each println (every 1,000 allocation), I put:
System.out.println("total mem:"+rt.totalMemory() );
System.out.println("free mem: "+rt.freeMemory() );





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us