UWP Unity app Bad Performance

marck0zz 26 Reputation points
2021-04-10T20:21:04.767+00:00

Hello all.

I have this Unity app that I'm developing for Windows standalone and UWP(Win10). Both works fine but in the UWP one, the more you use the app, the worst the performance goes.

In the app the user can import (from local disk) an image or folder with images to loade them in a 3D model. This works as it should be, BUT, every time I copy a new folder to the ".ApplicationData.Current.LocalFolder" the use of the CPU increse a lot. Have a look on the image of my Task Manager (the highlited one), after every "import" process. For the Win Standalone this doesn't happens.

86528-taskmanageruwp.png

Since the only different between both builds is basically the import folder process (copy to local folder) -Windows APIs (like Windows.Storage) vs System.IO- my guess is that my code is causing this intense use of CPU (I'm new on async/await/Task system).

Is this code that CPU intensive? And why it doesn't decrese after every import process?

//******************Check folder size******************  
async Task CheckFolderSize(Windows.Storage.StorageFolder thisFolder, CancellationToken token_)  
{  
	if (token_.IsCancellationRequested)  
		return;  
  
    if (timer.Elapsed.TotalSeconds > 60)  
	{  
		if(modelFault == "error_007")  
			return;  
		else  
		{  
			modelFault = "error_007";  
            Debug.LogError("Assing er007");  
            return;  
        }  
    }  
  
    Debug.LogError("Task has started");  
  
    List<Task> tasks = new List<Task>();  
  
    await FileSize(thisFolder, token_);  
  
    Debug.LogError("Get files size done.");  
  
    foreach (var folders in await thisFolder.GetFoldersAsync())  
    {  
        if (timer.Elapsed.TotalSeconds > 60)  
        {  
            if (modelFault == "error_007")  
                break;  
            else  
            {  
                modelFault = "error_007";  
                Debug.LogError("Assing er007");  
                break;  
            }  
        }  
        else  
        {  
            tasks.Add(CheckFolderSize(folders, token_));  
            Debug.LogError("One folder found at " + folders.Path);  
        }  
  
        if (token_.IsCancellationRequested)  
			return;  
    }  
  
    await Task.WhenAll(tasks);  
  
    timeElapsed += timer.Elapsed.TotalSeconds;  
  
    Debug.LogError("Task has DONE in: " + timer.Elapsed.TotalSeconds + "seg");  
}  
  
async Task FileSize(Windows.Storage.StorageFolder inFolder, CancellationToken token_)  
{  
    Debug.LogError("Time elapsed : " + timer.Elapsed.TotalSeconds + "seg");  
    if (token_.IsCancellationRequested)  
        return;  
  
    if (timer.Elapsed.TotalSeconds > 60)  
    {  
        if (modelFault == "error_007")  
            return;  
        else  
        {  
		    modelFault = "error_007";  
            Debug.LogError("Assing er007");  
            return;  
        }  
    }  
    else  
    {  
        var fileSizeTasks = (await inFolder.GetFilesAsync()).Select(async file => (await file.GetBasicPropertiesAsync()).Size);  
                          
        var sizes = await Task.WhenAll(fileSizeTasks);  
        var thisSize = sizes.Sum(l => (long)l);  
        folderSize += thisSize;  
  
        Debug.LogError("One Task Done");  
    }  
}  
  
  
//******************Copy from local disck to LocalFolder******************  
async Task CopyFolder(Windows.Storage.StorageFolder folderToCopy, Windows.Storage.StorageFolder targetFolder, CancellationToken token1)  
{  
    if (token1.IsCancellationRequested)  
        return;  
  
    if (modelsCount > 1)  
    {  
		if (modelFault == "error_004")  
			return;  
		else  
		{  
			modelFault = "error_004";  
			Debug.LogError("Assing er004");  
			return;  
		}  
	}  
  
    //if (!await FolderExistAsync(folderToCopy.Name, targetFolder))  
    //    await targetFolder.CreateFolderAsync(folderToCopy.Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);  
  
    if (!FolderExist(targetFolder.Path, folderToCopy.Name))  
    await targetFolder.CreateFolderAsync(folderToCopy.Name, Windows.Storage.CreationCollisionOption.ReplaceExisting);  
  
    Windows.Storage.StorageFolder destination = await targetFolder.GetFolderAsync(folderToCopy.Name);  
  
    Debug.LogError("folder created: " + destination.Path);  
  
    foreach (var file in await folderToCopy.GetFilesAsync())  
    {  
        if (token1.IsCancellationRequested)  
            return;  
  
        if (modelsCount > 1)  
        {  
            if (modelFault == "error_004")  
                return;  
            else  
            {  
                modelFault = "error_004";  
                Debug.LogError("Assing er004");  
                return;  
            }  
        }  
  
        string ext = Path.GetExtension(file.Name);  
  
        HashSet<string> set = new HashSet<string>() { ".gltf", ".glb", ".bin", ".png", ".jpg", ".jpeg", ".bmp" };  
        if (set.Contains(ext))  
        {  
            if (ext == ".gltf" || ext == ".glb")  
            {  
                modelsCount++;  
                Debug.LogError("gltf count + 1");  
  
                if (modelsCount > 1)  
                {  
                    modelFault = "error_004";  
                    Debug.LogError("model = error04");  
                    break;  
                }  
                else  
                {  
                    path_ = Path.Combine(destination.Path, file.Name);  
                    string writePath = path_;  
  
                    writePathObj = writePath;  
                    importedName = Path.GetFileNameWithoutExtension(file.Name);  
  
                    Debug.LogError("GLTF File: " + writePathObj);  
                }  
            }  
            await file.CopyAsync(destination, file.Name, Windows.Storage.NameCollisionOption.ReplaceExisting);  
        }  
        else  
        {  
            Debug.LogError("Trash File: " + file.Path);  
        }  
    }  
  
    foreach (var newFolder in await folderToCopy.GetFoldersAsync())  
    {  
        if (token1.IsCancellationRequested)  
            break;  
  
        if (modelsCount > 1)  
        {  
            if (modelFault == "error_004")  
                break;  
            else  
			{  
                modelFault = "error_004";  
                Debug.LogError("Assing er004");  
                break;  
            }  
        }  
        await CopyFolder(newFolder, destination, token1);  
    }  
}  
  
bool FolderExist(string destination, string foldername)  
{  
    string FolderPath_ = Path.Combine(destination, foldername);//Get the full path to the new model folder  
    if (Directory.Exists(FolderPath_))  
        return true;  
    else  
		return false;  
}  

I'll thank any help or advice.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,540 questions
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. AryaDing-MSFT 2,841 Reputation points
    2021-04-12T09:10:15.753+00:00

    Hi,

    Welcome to Microsoft Q&A!

    There are much code about getting the file size, which will also affect the memory. Could you please try to optimize the copy process? I suggest you could customize a method to copy all files and subfolders in the folder to ApplicationData.Current.LocalFolder like the following.

    Note that declare the broadFileSystemAccess capability in package.appxmainfest.

    private async void Button_Click(object sender, RoutedEventArgs e)  
            {  
                
                StorageFolder assetsFolder = await StorageFolder.GetFolderFromPathAsync(@"D:\Test Folder");  
                
               var folder= ApplicationData.Current.LocalFolder;  
      
               await CopyFolderAsync(assetsFolder, ApplicationData.Current.LocalFolder,null);  
            }  
      
            public static async Task CopyFolderAsync(StorageFolder source, StorageFolder destinationContainer, string desiredName = null)  
            {  
                StorageFolder destinationFolder = null;  
                destinationFolder = await destinationContainer.CreateFolderAsync(  
                    desiredName ?? source.Name, CreationCollisionOption.ReplaceExisting);  
      
                foreach (var file in await source.GetFilesAsync())  
                {  
                    await file.CopyAsync(destinationFolder, file.Name, NameCollisionOption.ReplaceExisting);  
                }  
                foreach (var folder in await source.GetFoldersAsync())  
                {  
                    await CopyFolderAsync(folder, destinationFolder);  
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful