int threads = 8;
// need min 80% free memory after test campared with
// beginning, else test fails
int minimalFreeMemAfterTest = 80;
PersistenceBroker broker = null;
try
{
MetadataManager mm = MetadataManager.getInstance();
// enable the per thread changes of metadata
mm.setEnablePerThreadChanges(true);
defaultRepository = mm.copyOfGlobalRepository();
mm.setDescriptor(defaultRepository);
ClassDescriptor cld;
long memoryUseBeforeTest;
long memoryUseAfterTest;
try
{
// prepare for test
long period = System.currentTimeMillis();
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
cld = broker.getClassDescriptor(targetTestClass);
// we manipulate the schema name of the class
// thus we note the original value
oldTestObjectString = cld.getFullTableName();
broker.close();
// cleanup JVM
Runtime.getRuntime().gc();
Thread.sleep(200);
Runtime.getRuntime().gc();
// start test
long memory = Runtime.getRuntime().freeMemory();
long totalMemory = getTotalMemory();
int count = 0;
for (int k = 0; k < loops; k++)
{
TestCaseRunnable tct [] = new TestCaseRunnable[threads];
for (int i = 0; i < threads; i++)
{
if (i % 2 == 0)
tct[i] = new ThreadedUsingBroker(loops);
else
tct[i] = new GlobalUsingBroker(loops);
}
// run test classes
runTestCaseRunnables(tct);
++count;
System.out.println("Free/total Memory after loop " + count + ": "
+ convertToMB(Runtime.getRuntime().freeMemory())
+ "/" + convertToMB(getTotalMemory()) + "MB");
}
period = System.currentTimeMillis() - period;
System.out.println(ClassUtils.getShortClassName(MetadataMultithreadedTest.class) + " take: "
+ period + " ms for " + loops + " loops, creating each with " + threads + " threads");
System.out.println("Free/total Memory before test: "
+ convertToMB(memory) + "/" + convertToMB(totalMemory) + "MB");
Runtime.getRuntime().gc();
Thread.sleep(200);
Runtime.getRuntime().gc();
Runtime.getRuntime().gc();
memoryUseBeforeTest = convertToMB(memory);
memoryUseAfterTest = convertToMB(Runtime.getRuntime().freeMemory());
System.out.println("Free/total Memory after test and gc: "
+ memoryUseAfterTest
+ "/" + convertToMB(getTotalMemory()) + "MB");
System.out.println("Do cleanup now ...");
}
finally
{
MetadataManager.getInstance().setEnablePerThreadChanges(false);
}
// get new PB instance
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
cld = broker.getClassDescriptor(targetTestClass);
String name = cld.getFullTableName();
assertEquals(oldTestObjectString, name);
assertFalse(MetadataManager.getInstance().isEnablePerThreadChanges());
double d = ((double) memoryUseAfterTest) / ((double) memoryUseBeforeTest);
int result = (int) (d * 100);
if (result < minimalFreeMemAfterTest)
{
fail("** When using a offical version of OJB, ignore this failure! **" +
" Memory usage after this test differs more than "+(100 - minimalFreeMemAfterTest)
+"% from beginning, this may indicate" +
" a memory leak (GC can't free unused metadata objects), but this could also be a result" +
" of your JVM settings. Please re-run test.");
}
}
finally
{
if (broker != null) broker.close();
}
}