classification
Title: os.makedirs() introduces high memory usage for explorer.exe
Type: Stage: resolved
Components: Windows Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: kevin.kuan.trend, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2020-10-14 04:52 by kevin.kuan.trend, last changed 2020-10-14 06:56 by kevin.kuan.trend. This issue is now closed.

Files
File name Uploaded Description Edit
Image20201014125025.png kevin.kuan.trend, 2020-10-14 04:52
Messages (2)
msg378603 - (view) Author: Kevin Kuan (kevin.kuan.trend) * Date: 2020-10-14 04:52
Hi,
I would like to report bug for os.makedirs().
I am running this script on Windows 10 1909 (most win10 work), python 3.8.1.

 
os.makedirs() is making explorer.exe huge amount of memory and crashing the system after only 3 hours.
After changing that line to subprocess.run('mkdir ...') memory usage is reduced significantly.After changing all shutil functions as well, memory usage will not grow. 

(This is my first time community contribution. If anything, please let me know.)
Kevin Kuan 
(kevin.kuan.trend@gmail.com)


-------script.txt------
import time
import logging
import uuid
import subprocess

REPEAT = 10

def new_target_folder():
        TARGET_BASE,
        str(uuid.uuid4().hex)
    )
    os.makedirs(target)
    return target


def delete_folder(target):
    if os.path.exists(target):
        shutil.rmtree(target)



def copy_samples(target):
    shutil.copytree(
        SOURCE_FOLDER, 
        os.path.join(target, 'file_copy_clean_samples')


def copy_file_test():
    for i in range(REPEAT):
        t = new_target_folder()
        copy_samples(t)
        delete_folder(t)

------after.txt------
import os
import shutil
import time
import logging
import uuid
import subprocess

REPEAT = 10

SOURCE_FOLDER = os.path.abspath(
    os.path.join(
        __file__,
        '..',
        '..',
        '_VolumeTestSamples',
        'file_copy_clean_samples',
    )
)

TARGET_BASE = os.path.join(
    os.environ['USERPROFILE'],
    r'Desktop',
    r'sample_file_copy',
    str(uuid.uuid4().hex)
)

PAUSE = 1


def run_for_one_week(func, pause):
    time_start = time.time()
    while True:
        time_now = time.time()
        logging.debug('{}'.format(time_now))
        if time_now - time_start > 1 * 7 * 24 * 60 * 60:
            break
        func()
        time.sleep(pause)


def new_target_folder():
    target = os.path.join(
        TARGET_BASE,
        str(uuid.uuid4().hex)
    )
    subprocess.run(
        ['mkdir', target],
        shell=True
    )
    return target


def delete_folder(target):
    if os.path.exists(target):
        subprocess.run(
            ['rmdir', '/s', '/q', target],
            shell=True
        )


def copy_samples(target):
    subprocess.run(
        ['echo', 'D', '|', 'xcopy', '/s', '/y', SOURCE_FOLDER, target],
        shell=True
    )


def copy_file_test():
    for i in range(REPEAT):
        t = new_target_folder()
        print (t)
        copy_samples(t)
        delete_folder(t)
    

if __name__ == '__main__':
    r = logging.getLogger()
    r.setLevel(logging.DEBUG)
    run_for_one_week(copy_file_test, PAUSE)
msg378605 - (view) Author: Kevin Kuan (kevin.kuan.trend) * Date: 2020-10-14 06:56
After further investigation. It seems that explorer.exe will use lots of memory when folders are rapidly created. The code change simply slows down folder creation. Since it is unrelated to python. I am closing this case.
History
Date User Action Args
2020-10-14 06:56:37kevin.kuan.trendsetstatus: open -> closed
resolution: not a bug
messages: + msg378605

stage: resolved
2020-10-14 04:52:04kevin.kuan.trendcreate