public void testDocumentGced() throws Exception {
    VirtualFile vFile = createFile();
    PsiDocumentManagerImpl documentManager = getPsiDocumentManager();
    long id = System.identityHashCode(documentManager.getDocument(getPsiManager().findFile(vFile)));

    documentManager.commitAllDocuments();
    UIUtil.dispatchAllInvocationEvents();
    UIUtil.dispatchAllInvocationEvents();
    assertEmpty(documentManager.getUncommittedDocuments());

    LeakHunter.checkLeak(documentManager, DocumentImpl.class);
    LeakHunter.checkLeak(
        documentManager,
        PsiFileImpl.class,
        new Processor<PsiFileImpl>() {
          @Override
          public boolean process(PsiFileImpl psiFile) {
            return psiFile.getViewProvider().getVirtualFile().getFileSystem()
                instanceof LocalFileSystem;
          }
        });
    // Class.forName("com.intellij.util.ProfilingUtil").getDeclaredMethod("forceCaptureMemorySnapshot").invoke(null);

    Reference<Document> reference = vFile.getUserData(FileDocumentManagerImpl.DOCUMENT_KEY);
    assertNotNull(reference);
    for (int i = 0; i < 1000; i++) {
      UIUtil.dispatchAllInvocationEvents();
      if (reference.get() == null) break;
      System.gc();
    }
    assertNull(documentManager.getCachedDocument(getPsiManager().findFile(vFile)));

    Document newDoc = documentManager.getDocument(getPsiManager().findFile(vFile));
    assertTrue(id != System.identityHashCode(newDoc));
  }
  public void testDocumentFromAlienProjectDoesNotEndsUpInMyUncommittedList() throws Exception {
    PsiFile file = getPsiManager().findFile(createFile());

    final Document document = getPsiDocumentManager().getDocument(file);

    File temp = createTempDirectory();
    final Project alienProject =
        createProject(new File(temp, "alien.ipr"), DebugUtil.currentStackTrace());
    boolean succ2 = ProjectManagerEx.getInstanceEx().openProject(alienProject);
    assertTrue(succ2);

    try {
      PsiManager alienManager = PsiManager.getInstance(alienProject);
      final String alienText = "alien";

      LightVirtualFile alienVirt = new LightVirtualFile("foo.java", alienText);
      final PsiFile alienFile = alienManager.findFile(alienVirt);
      final PsiDocumentManagerImpl alienDocManager =
          (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(alienProject);
      final Document alienDocument = alienDocManager.getDocument(alienFile);
      // alienDocument.putUserData(CACHED_VIEW_PROVIDER, new MockFileViewProvider(alienFile));
      assertEquals(0, alienDocManager.getUncommittedDocuments().length);
      assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);

      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                public void run() {
                  getPsiDocumentManager()
                      .documentChanged(
                          new DocumentEventImpl(
                              alienDocument,
                              0,
                              "",
                              "",
                              alienDocument.getModificationStamp(),
                              false));
                  assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(0, alienDocManager.getUncommittedDocuments().length);

                  alienDocManager.documentChanged(
                      new DocumentEventImpl(
                          alienDocument, 0, "", "", alienDocument.getModificationStamp(), false));
                  assertEquals(0, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);

                  getPsiDocumentManager()
                      .documentChanged(
                          new DocumentEventImpl(
                              document, 0, "", "", document.getModificationStamp(), false));
                  assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);

                  alienDocManager.documentChanged(
                      new DocumentEventImpl(
                          document, 0, "", "", document.getModificationStamp(), false));
                  assertEquals(1, getPsiDocumentManager().getUncommittedDocuments().length);
                  assertEquals(1, alienDocManager.getUncommittedDocuments().length);
                }
              });
    } finally {
      ProjectUtil.closeAndDispose(alienProject);
    }
  }