The performance difference is probably based simply on the fact that the
Java example has to do run-time garbage collection and the C++ version
doesn't have to do any memory management in your example.  Try allocating a
large number of heap items from C++ before deleting them. Right now you are
being overly nice to the heap allocation with C++ because you never allocate
more than one heap object at a time. There is no chance that you will get
close to filling up the heap or cause any sort of slowdown with your C++
example.

With Java you are constantly filling the run-time heap and then relying on
garbage collection to clean up after you. Your C++ code is in no way
equivalent to the Java code. In fact, in comparison to the real world, your
C++ code is pretty useless. No one uses a heap that way in the real world.


-----Original Message-----
From: skeptical [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, August 30, 2000 6:31 AM
To: [EMAIL PROTECTED]
Subject: slow memory allocation problem


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: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to