thanks for the detailed response. is there any way to solve this problem?
this would be a major bottleneck for heavy network server apps where packets
has to be read into buffer and released when done processing.
tks,
peter
----- Original Message -----
From: "Bill Hackmann" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, August 30, 2000 7:24 PM
Subject: Re: slow memory allocation problem
> You have discovered one of the fundamental differences between Java and
C++;
>
> In C++, when you allocate and then delete a block of memory, it is
immediately
> combined with adjacent free blocks, and the free memory chain can be
scanned
> quite quickly;
>
> In your case, you simply dereference an array, which will not be garbage
> collected until
> 1) Some free time is available (never true since you thread will have
higher
> priority as long as memory is available)
> 2) Your allocation request cannot be satisfied
>
> Eventually, you are probably running garbage collection every couple of
loops;
> GC can be slow, and is very implementation dependent;
>
> If you want the slower sequence at the start of your run, run your Java
program
> loop backwards, from NUM_MAX_BYTES to 1;
>
> This is an inherent result of using Java's chosen memory model.
>
> If you were only interested in reducing the variance, if you waited a
short
> will after each allocation (Thread.sleep(1)) you might find the variance
> reduced, but Java would take a minimum of NUM_MAX_BYTES milliseconds to
run, 5
> minutes in your example. Also, I am not sure at all how Thread.sleep(n)
works
> on a platform where the clock resolution is low, as on most Windows
platforms.
>
>
> At 06:21 PM 8/30/00 +0800, you wrote:
> >guys:
> >
> >i am doing a benchmark on java memory management. i wrote an app to
allocate
> >and free memory from 1 to 300k bytes. it runs forever. i notice it takes
> >longer to allocate additional memory as time goes on. the same app only
> >takes 9 seconds on C++. here's the code listing. can anyone pls help out?
> >
> >tks,
> >peter
> >
> >=======JAVA CODE LISTING =======
> >public class Class1
> >{
> > private static final int NUM_MAX_BYTES = 300000;
> >
> > public static void main (String[] args)
> > {
> > int i;
> > long t1;
> >
> > t1 = System.currentTimeMillis();
> >
> > System.err.print("memory alloc/free 1 - "+NUM_MAX_BYTES+" bytes. ");
> > for (i = 0; i < NUM_MAX_BYTES; i++)
> > {
> > byte[] p;
> > p = new byte[i];
> > if (i % 1000 == 0)
> > {
> > // Runtime rt = Runtime.getRuntime();
> > // rt.gc();
> > System.err.println("current: "+i);
> > }
> > }
> >
> > System.err.println("time taken: %d "+ (System.currentTimeMillis()-t1) +
> >"ms");
> >
> > }
> >}
> >
> >
> >======= C++ CODE LISTING =========
> >
> >#include "stdafx.h"
> >#include <stdio.h>
> >#include <windows.h>
> >
> >#define NUM_MAX_BYTES 300000
> >
> >int main(int argc, char* argv[])
> >{
> > int i;
> > DWORD t1;
> > t1 = GetTickCount();
> >
> > BYTE * p;
> > printf("memory alloc/free 1 - %d bytes. ", NUM_MAX_BYTES);
> > for (i = 0; i < NUM_MAX_BYTES; i++)
> > {
> > p = new BYTE[i];
> > delete p;
> > }
> > printf("time taken: %d ms\n",GetTickCount()-t1);
> >
> > return 0;
> >}
> >
>
>___________________________________________________________________________
> >To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> >of the message "signoff SERVLET-INTEREST".
> >
> >Archives: http://archives.java.sun.com/archives/servlet-interest.html
> >Resources: http://java.sun.com/products/servlet/external-resources.html
> >LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html