Skip to main content

Delete a specific resource from VRAM?

Answered

Comments

1 comment

  • Jason Stanczyk

    Delete a specific resource form VRAM?
    Will unregisteredResource() remove the resource from VRAM or just delete the reference?

    There is ResourceManager::purge()
    This function will remove all ResourceManager's references to Resources that are not referencef from somewhere else.
    A common pitfall with purge, is that purge will not remove references if there is at least one other strong pointer to that Resource.
    A common way around this is to wait at least one mainloop iteration before calling purge, so that all Kanzi's internal and cached references are cleared.
     
    ResourceManager::unregisterResource() removes the reference from ResourceManager to the Resource Object.
    The Resource is destroyed when there are no longer any shared_ptr that references it.

     

    Here is an example code hot to make sure a single Resource is destroyed, and how to verify. 

     

    const char* TEXTURE_URL = "file://./Image1.png";

    // Application class.
    // Implements application logic.
    class DeleteResource19059Application : public ExampleApplication
    {
    protected:

    // Configures application.
    void onConfigure(ApplicationProperties& configuration) override
    {
    configuration.binaryName = "deleteresource19059.kzb.cfg";
    configuration.performanceInfoLevel = ApplicationProperties::PerformanceInfoLevelFull;
    }

    // Initializes application after project has been loaded.
    void onProjectLoaded() override
    {
    // Code to run after the .KZB has been loaded.

    getRoot()->lookupNode<Button2D>("Stack Layout 2D/SetButton")->addMessageHandler(ButtonConcept::ClickedMessage, bind(&DeleteResource19059Application::onSetButton, this, placeholders::_1));
    getRoot()->lookupNode<Button2D>("Stack Layout 2D/DeleteButton")->addMessageHandler(ButtonConcept::ClickedMessage, bind(&DeleteResource19059Application::onDeleteButton, this, placeholders::_1));
    }

    void onSetButton(ButtonConcept::ClickedMessageArguments& args)
    {
    /*
    * Print the current mem-consumption
    */
    size_t count, cpumem, gpumem;
    getResourceManager()->getMemoryUsage(&count, &cpumem, &gpumem);
    kzLogInfo(KZ_LOG_CATEGORY_GENERIC, ("Before acquireResource(TEXTURE_URL) : getMemoryUsage count: {}, cpumem: {}, gpumem: {}", count, cpumem, gpumem));

    /*
    * this adds a reference from the ResourceManager to the Resource Object.
    */
    TextureSharedPtr texture = getResourceManager()->acquireResource<Texture>(TEXTURE_URL);

    /*
    * this adds reference from the Image2D node to the Resource Object.
    */
    getRoot()->lookupNode<Image2D>("Image")->setImage(texture);
    }

    void onDeleteButton(ButtonConcept::ClickedMessageArguments& args)
    {
    /*
    * this removes the reference from the ResourceManager to the Resource Object.
    * without this removal, the Resource Object will not become unreferenced.
    */
    getResourceManager()->unregisterResource(TEXTURE_URL);

    /*
    * this removes the reference from the Image2D node to the Resource Object.
    * without this removal, the Resource Object will not become unreferenced.
    */
    getRoot()->lookupNode<Image2D>("Image")->setImage(nullptr);

    /*
    * The Resource Object unreferencing might not be immediate,
    * it might take a mainloop iteration until all references are lost.
    */
    size_t count, cpumem, gpumem;
    getResourceManager()->getMemoryUsage(&count, &cpumem, &gpumem);
    kzLogInfo(KZ_LOG_CATEGORY_GENERIC, ("After setImage(nullptr) : getMemoryUsage count: {}, cpumem: {}, gpumem: {}", count, cpumem, gpumem));
    }

     

    If you are observing that the memory consumption does not go down, then it is a sign that Resources are not destroyed.

     

    1

Please sign in to leave a comment.