Out of memory error while working with ReferenceKey

Out of memory error while working with ReferenceKey

amitabhVA4SD
Advocate Advocate
2,741 Views
23 Replies
Message 1 of 24

Out of memory error while working with ReferenceKey

amitabhVA4SD
Advocate
Advocate

Hello All,

 

I am experiencing an out-of-memory error while working with Topology Tagging APIs on Inventor 2020.3.4. 

The Assembly is loaded in "Load Full" mode since the "Express Mode" does not give access to the Topology/B-Rep information.
I am getting this issue while trying to collect the B-rep topology using ReferenceKey and ReferenceKeyContext APIs for each Face and Edge in the Assembly using the C# .Net external application. I need this topology persistent id information to bind back this data in a disconnected Inventor Add-In application.
The system has 16 GB RAM and we did check the GDI limit as well.
We found the memory to exhaust at ~33,000 keys.
We are working with one ReferenceKeyManager and Multiple Key Context Tables
 
Please see the image below from the Task Manager showing the GDI count and Memory consumed while running the application. It is at this exact moment that the out of memory error is being thrown and Inventor crashes
 

Out of memory task manager.jpg

 
A piece of the code implementation is also attached
 

 

[TestMethod]
        public void ReferenceKeyApi()
        {
            // Get Inventor App
            InventorApp = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
            VctApplication.InventorApp = InventorApp;
            Document document = VctApplication.InventorSession.ActiveDoc;
            SelectSet selectSet = document.SelectSet;
            dynamic selectedObject = null;
            if (selectSet.Count > 0)
                 selectedObject = selectSet[1];
       
            // Initialize the VctDocument class
            VctDocument vctDocument = VctApplication.InventorSession.VctDocument;

            // Example for an assembly document
            switch (selectedObject)
            {
                case FaceProxy faceProxy:
                    faceProxy = (FaceProxy) selectedObject;

                    // To get reference key and key context
                    string fKey = vctDocument.GetReferenceKey(document, faceProxy, out string fKeyContext);
                    selectSet.Clear();

                    // later to bind back the reference key to the entity using the key context
                    FaceProxy fRetrievedObject = vctDocument.GetEntityFromReferenceKey(document, fKey, fKeyContext);
                    selectSet.Select(fRetrievedObject);
                    break;

                case EdgeProxy edgeProxy:
                    edgeProxy = (EdgeProxy) selectedObject;

                    // To get reference key and key context
                    string eKey = vctDocument.GetReferenceKey(document, edgeProxy, out string eKeyContext);
                    selectSet.Clear();

                    // later to bind back the reference key to the entity using the key context
                    EdgeProxy eRetrievedObject = vctDocument.GetEntityFromReferenceKey(document, eKey, eKeyContext);
                    selectSet.Select(eRetrievedObject);
                    break;
            }
        }

 

 

Thanks,

Amitabh Mukherjee

0 Likes
Accepted solutions (1)
2,742 Views
23 Replies
Replies (23)
Message 21 of 24

amitabhVA4SD
Advocate
Advocate

We have another question @CattabianiI 

There is an API called ReleaseKeyContext. When should this API be called? Is it at the very end of the binding back?

What does it do? We did not see this API being used in your sample program.

0 Likes
Message 22 of 24

tomas.bujnoch
Autodesk
Autodesk

To work with reference keys, the ReferenceKeyManager should be obtained from the Document object. There are two functions in the ReferenceKeyManager object that can create the key binding context scope hence allocate some memory:

long handle = ReferenceKeyManager.CreateKeyContext()

long handle = ReferenceKeyManager.LoadContextFromArray(something)

Both these functions provide a long integer as a handle to the corresponding key binding context scope and also both these functions need to be accompanied by the call to:

ReferenceKeyManager.ReleaseKeyContext(handle)

After this call, the document key binding scope should be released.

It might look cumbersome, but creating or restoring a new document key binding scope every time something needs to be bound to the reference key is a relatively expensive operation, so keeping it alive via handle helps with performance. A lot.

Message 23 of 24

amitabhVA4SD
Advocate
Advocate

Thank you @CattabianiI @MjDeck @tomas.bujnoch @YuhanZhang .

We have found the root cause of the problem, the issue was to do with the Excel cell character limit issue. We are storing the information in an Excel file. The Excel cell is able to hold only 32767 characters, whereas the actual key context is 746232 characters long. We are now storing this in a text file and we are able to bind the key back.

Thank you, again to all who contributed to the discussion and helped find the right approach to doing this.

 

Thanks,

Amitabh Mukherjee

 

Message 24 of 24

YuhanZhang
Autodesk
Autodesk

@amitabhVA4SD Glad to hear it was solved!



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes