LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
MRedRaider

True, full, on-demand garbage collection

Status: Declined

Any idea that has not received any kudos within a year after posting will be automatically declined. 

I've recently run into an issue when using external code (ActiveX to be exact) where I needed to explicitly call garbage collection in a similar fashion to how it's done in C# (C Sharp).

The "Request Deallocation" function isn't true garbage collection.

 

A C# example would be:

someComObject = null;

GC.Collect();

 

 

There are situations where it is neccessary to explicitly call garbage collection.

7 Comments
Knight of NI

Can you provide an example of such a situation?

MRedRaider
Active Participant

I cannot provide the code I have, because I don't own it. I can, however, describe a similar instance:

 

1. Register a system.speech based .nET callback. (perhaps LVSpeak? I've not tested it with Norm's code)

2. Trigger the event by speaking a recognized phrase.

3. Close ALL system.speech references but leave the Main application running.

4. Go to the control panel and try to delete the default speech profile (it's the one used by system.speech). You might want to back up that profile first or maybe create a new one for this test.

5. Experience an error that the profile is still in use.

 

Now, why does this have to do with ActiveX and COM? Because the API that allows you to modify those speech profiles that you just tried to delete are ONLY available in SAPI (COM) based speech API and are not exposed by Microsoft through System.Speech.

 

Everything I described above works fine in C#, so long as I call the garbage collector. If you don't, you get the same error that you get from LabVIEW stating that the speech profile is in use.

 

Now, the workaround? Attempt to delete the profile, ignore the error and try again. Apparently generating the error forces LabVIEW to cleanup the references and the second try works fine.

 

 

How many other .NET based event callbacks would be effected I cannot say. I can say that most applications would handle this situation by simply exiting versus closing references... but I'd argue that there are instances when you need to access the objects you were just using and true Garbage Collection is the only way this would be possible.

AristosQueue (NI)
NI Employee (retired)

If you need this, you should call the garbage collection function for the external system that you are using. LabVIEW can't possibly know what you're wanting to clean up. So create a Call Library node to call the CLR's garbage collector, or the ActiveX garbage collector, or the JAVA garbage collector, as needed by your application.

 

The "Request Deallocation" is not a garbage collector in any sense. It deallocates the dataspace of the VI that is still in use, which trades off lower memory for poorer performance when the VI is called a second time. It is for use only on subVIs that infrequently get a very large array moving through the diagram and will not be called a second time for a substantial period of time (since if they were called frequently, the deallocation of the cache would take place on the next call).

MRedRaider
Active Participant

Aristos:

 

I don't own the System.Speech libraries to call the GC, I guess I could write wrappers around them in C# and have LabVIEW call those wrappers... but I'd rather not add that extra layer if I don't have to.

 

I wondered if the issue that I'm describing might be more of a bug than a request... since it has to do with .NET callbacks not getting completely cleaned up in LabVIEW.

AristosQueue (NI)
NI Employee (retired)

> I wondered if the issue that I'm describing might be more of a bug than

> a request... since it has to do with .NET callbacks not getting completely

> cleaned up in LabVIEW.

 

It sounds like a bug in someone's code, but it sounds more like a problem in MS speech APIs, not LV. If it is a problem of LV holding onto a reference, invoking the garbage collector wouldn't fix the problem because that item wouldn't be cleaned up. Since calling the garbage collector does fix the problem (I assume, since that's the idea you posted), it seems like the problem is in the speech system's clean up code. Obviously, that would take more analysis to confirm. If you can share your code, you might call an Application Engineer and see if they can dig into this issue deeper.

 

In any case, as far as the idea is concerned, I stand by my statement: if you need the garbage collection for some system to be explicitly invoked, you need to call a library function to do that. LV can't just create such a library function out of thin air.

MRedRaider
Active Participant

Aristos:

 

Agreed and noted. My point in mentioning the third party code was that we won't always own the other code and having garbage cleanup in our "native language" would be really handy. I have a rule: "Why do in C# what I can do faster and easier in LabVIEW"

 

I already spoke to an AE, they reviewed the workaround I found for my situation with R&D and suggested I post the GC idea here 🙂

 

Thanks for your responses!

Darren
Proven Zealot
Status changed to: Declined

Any idea that has not received any kudos within a year after posting will be automatically declined.