0 Replies Latest reply on Jul 22, 2015 1:45 AM by darrenten

    Memory leak while exporting indd to pdf file

    darrenten Level 1

      Hi,

       

      I found memory leak while converting indd file to pdf.

      I export a 2.21 MB indd file and it will leak about 200kb per time.

      I have reset some 'big' variables null to release and even called $.gc() but it doesn't work at all.

      What's the problem.

       

      Code is below:

      var result = "No documents are open................";

      // var argsNames=app.scriptArgs.getValue("Name").split(";")

      argsNames = arguments;

      var fileName = argsNames[0];

      var argPreset;

      var storeLocation;

      if (argsNames && argsNames[1]) {

          argPreset = argsNames[1];

      }

      if (argsNames && argsNames[2]) {

          storeLocation = argsNames[2];

      }

      Log("Params:" + fileName + ";" + argPreset + ";" + storeLocation)

      //OpenOptions.DEFAULT_VALUE

      //OpenOptions.OPEN_ORIGINAL

      //OpenOptions.OPEN_COPY

      var myDocument = app.open(new File(fileName),OpenOptions.OPEN_COPY);

      //Fill in a valid file path on your system.

      var myFolder = new Folder(storeLocation);

      var result = "saved to folder:  " + myFolder.fullName;

      if (!myFolder.exists && !myFolder.create()) {

          result = "Not saved.  Unable to create the folder:  " + myFile.parent.fullName;

      } else {

          //myExport2SepratedPages(myDocument, myFolder.fullName, argPreset);

          myExportAllPages(myDocument, myFolder.fullName, argPreset);

      }

      //myDocument.close();

      myDocument = null;

      //var endMark=app.performanceMetric(PerformanceMetricOptions.currentMemoryMark);

      //app.dumpBetweenMemoryMarks(startMark,endMark);

      result+=($.summary());

      $.gc();

       

      //export to a single pdf file

      function myExportAllPages(myDocument, myFolderName, presetFile) {

          var start = new Date();

          var myBaseName = myDocument.name;

          myPageName = myDocument.pages.item(0).name;

          app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;

          app.importFile(ExportPresetFormat.DOCUMENT_PRESETS_FORMAT, presetFile);

          //Generate a file path from the folder name, the base document name, and the page name.

          //Replace the colons in the page name (e.g., "Sec1:1") wtih underscores.

          var myRegExp = new RegExp(":", "gi");

          myPageName = myPageName.replace(myRegExp, "_");

        //test

        var sufix=Math.random()*100000;

       

          myFilePath = myFolderName + "/" + myBaseName + "_" + myPageName +sufix+ ".pdf";

          myFile = new File(myFilePath);

          myDocument.exportFile(ExportFormat.pdfType, myFile);

        myDocument.close(SaveOptions.NO);

          myDocument = null;//delete document

          var end = new Date();

       

          Log("Start:" + start + ";End:" + end)

      }

       

      function GetScriptPath() {

          // This function returns the path to the active script, even when running ESTK

          try {

              return app.activeScript.parent.fsName;

          } catch (e) {

              return File(e.filePath.parent.fsName);

          }

      }

       

       

      function Log(message) {

          var today = new Date();

          var logFile = new File(GetScriptPath() + "/myLog.txt");

          if (!logFile.exists) {

              // make new log file

              logFile.open("w");

              logFile.write(String(today) + "\nThe following messages were logged:\n");

              logFile.close();

          }

          logFile.open("e");

          logFile.seek(0, 2);

          logFile.write("\n" + message);

          logFile.close();

           logFile=null;

      }