This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch 
bugfix/371-Repeated-creation-of-type-systems-can-exhaust-JVM-metaspace
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit 7482ab042784c0c9f26840a208e0b3d4780bd3b8
Author: Richard Eckart de Castilho <[email protected]>
AuthorDate: Thu Aug 15 17:54:07 2024 +0200

    Issue #371: Repeated creation of type systems can exhaust JVM metaspace
    
    - Adding a "test case" - actually needs manual observation at the moment
---
 .gitignore                                         |  1 +
 .../apache/uima/cas/impl/TypeSystemImplTest.java   | 39 +++++++++++++++-------
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore
index a7ddf4288..3a62d2cae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,4 @@ api-change-report
 issuesFixed
 .idea
 *.iml
+.tycho-consumer-pom.xml
diff --git 
a/uimaj-core/src/test/java/org/apache/uima/cas/impl/TypeSystemImplTest.java 
b/uimaj-core/src/test/java/org/apache/uima/cas/impl/TypeSystemImplTest.java
index 471379fd7..9e0cea722 100644
--- a/uimaj-core/src/test/java/org/apache/uima/cas/impl/TypeSystemImplTest.java
+++ b/uimaj-core/src/test/java/org/apache/uima/cas/impl/TypeSystemImplTest.java
@@ -25,32 +25,31 @@ import static 
org.assertj.core.api.Assertions.assertThatExceptionOfType;
 import static org.assertj.core.api.Assertions.assertThatNoException;
 import static org.assertj.core.api.InstanceOfAssertFactories.throwable;
 
-import java.util.Iterator;
-
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.CASRuntimeException;
 import org.apache.uima.jcas.JCas;
 import org.apache.uima.jcas.JCasRegistry;
 import org.apache.uima.jcas.tcas.Annotation;
+import org.apache.uima.resource.impl.ResourceManager_impl;
 import org.apache.uima.resource.metadata.TypeSystemDescription;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import x.y.z.Sentence;
 
-public class TypeSystemImplTest {
+class TypeSystemImplTest {
   private TypeSystemDescription tsd;
 
   @BeforeEach
-  public void setup() {
+  void setup() {
     tsd = getResourceSpecifierFactory().createTypeSystemDescription();
   }
 
   @Test
-  public void thatTypeUsedInJavaAndDeclaredInTypeSytemDoesNotThrowException() 
throws Exception {
+  void thatTypeUsedInJavaAndDeclaredInTypeSytemDoesNotThrowException() throws 
Exception {
     tsd.addType(Sentence._TypeName, "", CAS.TYPE_NAME_ANNOTATION);
 
-    JCas localJcas = createCas(tsd, null, null).getJCas();
+    var localJcas = createCas(tsd, null, null).getJCas();
     localJcas.setDocumentText("This is a test.");
 
     assertThatNoException() //
@@ -58,8 +57,8 @@ public class TypeSystemImplTest {
   }
 
   @Test
-  public void thatTypeUsedInJavaButNotDeclaredInTypeSytemThrowsException() 
throws Exception {
-    JCas localJcas = createCas(tsd, null, null).getJCas();
+  void thatTypeUsedInJavaButNotDeclaredInTypeSytemThrowsException() throws 
Exception {
+    var localJcas = createCas(tsd, null, null).getJCas();
     localJcas.setDocumentText("This is a test.");
 
     
assertThat(JCasRegistry.getClassForIndex(Sentence.type)).isSameAs(Sentence.class);
@@ -74,8 +73,8 @@ public class TypeSystemImplTest {
   }
 
   @Test
-  public void thatTypeNotInTypeSystemAndWithoutJCasClassThrowsException() 
throws Exception {
-    JCas localJcas = createCas(tsd, null, null).getJCas();
+  void thatTypeNotInTypeSystemAndWithoutJCasClassThrowsException() throws 
Exception {
+    var localJcas = createCas(tsd, null, null).getJCas();
     localJcas.setDocumentText("This is a test.");
 
     assertThatExceptionOfType(CASRuntimeException.class) //
@@ -87,13 +86,29 @@ public class TypeSystemImplTest {
     sanityCheckForCasConsistencyUIMA_738(localJcas);
   }
 
-  private void sanityCheckForCasConsistencyUIMA_738(JCas localJcas) {
+  void sanityCheckForCasConsistencyUIMA_738(JCas localJcas) {
     // check that this does not leave JCAS in an inconsistent state
     // (a check for bug UIMA-738)
-    Iterator<Annotation> iter = localJcas.getAnnotationIndex().iterator();
+    var iter = localJcas.getAnnotationIndex().iterator();
+
     assertThat(iter).hasNext();
     assertThat(iter.next()) //
             .extracting(Annotation::getCoveredText) //
             .isEqualTo("This is a test.");
   }
+
+  @Test
+  void provokeMetaspaceExhaustion() throws Exception {
+    var type = tsd.addType(Sentence._TypeName, "", CAS.TYPE_NAME_ANNOTATION);
+    type.addFeature(Sentence._FeatName_sentenceLength, null, 
CAS.TYPE_NAME_INTEGER);
+
+    for (var i = 0; i < 100_000_000l; i++) {
+      var resMgr = new ResourceManager_impl();
+      resMgr.setExtensionClassPath(".", false);
+      createCas(tsd, null, null).getJCas();
+      System.runFinalization();
+      // Make sure the consolidated type system is evicted from the weak 
hashmap cache
+      System.gc();
+    }
+  }
 }

Reply via email to