question

larswissler-5584 avatar image
0 Votes"
larswissler-5584 asked combachi-7801 answered

Word Addin high CPU and force close after storing customProperties.

In my Word Addin I need to store relations between keys, values and content control ids. Currently I store these relations in document.properties.customProperties. I use the key as the customProperty key and add the value and ids as stringified JSON to load and reuse during future Addin usages. This is the storage code:

 private updateCustomProperties(normalizedData: Array<{ [key: string]: string; }>): Promise<void> {
      return Word.run(async (context) => {

          normalizedData.forEach(async newProp => {

              //create object with new value
              let updateObj = { value: newProp.value };

              //get content control ids. It is an array of numbers
              let ids;
              for (const listItem of this.linkListToCustomProperty) {
                if (listItem.varName === newProp.key) {
                  ids = listItem.ccIds;
                  break;
                }
          
              if (typeof ids !== 'undefined') {

                //add ids to update object
                updateObj['ccIds'] = ids;

                 //set custom property to the update object
                context.document.properties.customProperties.add(newProp.key, JSON.stringify(updateObj));

              } 
                    
              //sync
              await context.sync();
                    
            }
          });
        
          context.document.properties.load('customProperties');
         await context.sync();
      });
    }


The issue: After that storage process and the complete Addin process is finished, Word will completely block one core of CPU for up to half an hour and be pretty much unusable. If I try to use Word during that time, i.e. save the document or scroll/type, often Word will crash. I am assuming that storage in customProperties triggers some sort of endless garbage collection. I am storing up to 1000 keys with their values and ids.

I have tried different cycle sizes before context.synch() but did not see a change to the better. The size of custom properties is limited to 256 chars so I have to store each key in a separate property. Any ideas would be very welcome. Is there a better place to store this kind of data to reuse after closing/reopening the document? Is there a way to circumvent that high usage?







office-addins-devoffice-js-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AbidRahman-1538 avatar image
0 Votes"
AbidRahman-1538 answered YihuaXiong-7572 commented

Thanks Sean.

Hey @larswissler-5584, is it possible to provide a working sample that reproduces this behavior as a gist that we can run using Script lab?
Does this occur on any document that you run this code in or only in specific documents?

Thanks,
Abid


· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @AbidRahman-1538 here is the link for the Gist Sample program runnable in Script Lab. If I try to import, I get a YAML error. But you can just copy/paste the code into a new Script Lab script, and it runs fine. https://gist.github.com/lw7M/dc0cfb9fd7c160946817457e98684243. It is independent of the document, I tested on the default/empty one.

I also did a bit more testing. With the data in the Gist and a completely empty document, the Sample runs for one second until finishing. Afterwards Word takes rather consistently ~15 % CPU load for 5 minutes on my PC (i5-8350U). The time seems to scale with the number of variables. If I reduce the sample size by one half, CPU load time drops to ~3 minutes.

Hope this is what you need. Please request more information any time.

1 Vote 1 ·

Thanks @larswissler-5584. We're able to see the same experience when testing on Windows. There is a bump in the CPU usage when running this code. We are investigating the issue (#5127591 for internal tracking), and will let you know as soon as we find out more.

1 Vote 1 ·

Hello @AbidRahman-1538, it occurs at least in so many documents that I cannot see a pattern. I will prepare a sample today and get back to you here.

0 Votes 0 ·

Hey @larswissler-5584,

After investigating the issue, it looks like we cannot avoid the high CPU usage when adding numerous custom properties. Every time a custom property is added, the application checks to see if a property with the same name already exists and if so, it is updated. As this is done in the low-level code of the application, it results in increased CPU usage.

Here is a possible suggestion to avoid this:
- Instead of adding the properties one by one, put the properties into an array (or a dictionary) first, then insert the entire array as a string into ONE custom property. This will require you (the add-in) to parse and extract the properties but can avoid the high CPU usage that you're seeing when attempting to add/modify several custom properties at once.

Please let me know if this workaround is sufficient.

Thanks,
Abid


0 Votes 0 ·

Hey @AbidRahman-1538 first thanks a lot for the quick feedback.

I originally tried to store all the data in one array. That is a much cleaner solution. But I had to learn there is a limit of 255 characters per property and causing the process to crash (source i.e. here https://social.msdn.microsoft.com/Forums/office/en-US/fb885674-dc8c-4c0a-9202-374adbf11612/length-of-docproperties-in-word-2010?forum=worddev). For that reason I rebuild the storing mechanism to store each pair in a separate property which led to the current issue.

0 Votes 0 ·
Show more comments
SeanLaberee-6205 avatar image
0 Votes"
SeanLaberee-6205 answered

@AbidRahman-1538 - This appears to be an issue with Word APIs. Can you have someone take a look?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

combachi-7801 avatar image
0 Votes"
combachi-7801 answered

i can not access document.properties. / document.customProperties - i use Delphi. not C. :-)
so i can not analyze your problem.....

but i think you can try document.variables instead.
each variable's value charactor limit to 254 too.
I hope this will alternative.


and i hope you review this my issue.
https://docs.microsoft.com/en-us/answers/questions/444826/how-insert-formatted-text-to-field-at-one-time.html

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.