Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiprocessing.Array do not release shared memory #76940

Closed
OOO mannequin opened this issue Feb 3, 2018 · 9 comments
Closed

multiprocessing.Array do not release shared memory #76940

OOO mannequin opened this issue Feb 3, 2018 · 9 comments
Labels
3.8 only security fixes OS-windows performance Performance or resource usage

Comments

@OOO
Copy link
Mannequin

OOO mannequin commented Feb 3, 2018

BPO 32759
Nosy @pfmoore, @pitrou, @vstinner, @tjguk, @zware, @serhiy-storchaka, @zooba, @applio
PRs
  • bpo-32759: Free unused arenas in multiprocessing.heap #5827
  • Files
  • result.jpg
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-04-09.15:42:05.582>
    created_at = <Date 2018-02-03.21:14:21.824>
    labels = ['3.8', 'OS-windows', 'performance']
    title = 'multiprocessing.Array do not release shared memory'
    updated_at = <Date 2020-04-23.22:20:04.619>
    user = 'https://bugs.python.org/OOO'

    bugs.python.org fields:

    activity = <Date 2020-04-23.22:20:04.619>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-04-09.15:42:05.582>
    closer = 'pitrou'
    components = ['Windows']
    creation = <Date 2018-02-03.21:14:21.824>
    creator = 'OO O'
    dependencies = []
    files = ['47424']
    hgrepos = []
    issue_num = 32759
    keywords = ['patch']
    message_count = 9.0
    messages = ['311571', '311573', '312626', '312628', '315131', '315132', '315133', '315137', '367153']
    nosy_count = 9.0
    nosy_names = ['paul.moore', 'pitrou', 'vstinner', 'tim.golden', 'zach.ware', 'serhiy.storchaka', 'steve.dower', 'davin', 'OO O']
    pr_nums = ['5827']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue32759'
    versions = ['Python 3.8']

    @OOO
    Copy link
    Mannequin Author

    OOO mannequin commented Feb 3, 2018

    OS: Win10 / 8.1
    Python: 3.5 / 3.6

    My program use mp.Array to share huge data.
    But suffer from out of memory after running for a while.

    But Windows task manager didn't show which process use that huge memory. And I use pympler to check my python memory usage. Still shows noting. So, I use RamMap to check, it shows a huge shared memory is used.

    I can reproduce the case by the simple test code:
    #-------------------------------------------------------

       import numpy as np
       import multiprocessing as mp
       import gc
    
       def F ():
           a = mp.Array ( 'I', 1800000000, lock = False )

    F ()
    gc.collect ()
    #-------------------------------------------------------

    No matter how hard I tried. the memory is not released.
    I put what I tried in the attachment picture.

    @OOO OOO mannequin added OS-windows performance Performance or resource usage labels Feb 3, 2018
    @OOO
    Copy link
    Mannequin Author

    OOO mannequin commented Feb 3, 2018

    mp.heap.BufferWrapper._heap = mp.heap.Heap ()
    gc.collect ()

    This is working!! The memory is cleared.
    Just delete the globe _heap and recreate a new one, but is the the correct way???

    @pitrou
    Copy link
    Member

    pitrou commented Feb 23, 2018

    Ok, this is because the multiprocessing Heap object never releases any unused arena objects, so the shared memory you allocate will probably stay allocated until the process tree ends.

    It is possible to change the strategy to delete unused arenas, though it's unsure whether doing so has adverse consequences (such as making later allocations costlier). It may make sense to only reclaim the larger arenas (larger than 1MB perhaps?).

    @pitrou pitrou added the 3.8 only security fixes label Feb 23, 2018
    @pitrou
    Copy link
    Member

    pitrou commented Feb 23, 2018

    It is also possible to uncommit the memory without deallocating it, making reuse potentially faster, but that requires low-level platform-specific code (e.g. madvise(MADV_DONTNEED) on Linux or DiscardVirtualMemory() on Windows).

    @pitrou
    Copy link
    Member

    pitrou commented Apr 9, 2018

    New changeset e4679cd by Antoine Pitrou in branch 'master':
    bpo-32759: Free unused arenas in multiprocessing.heap (GH-5827)
    e4679cd

    @pitrou
    Copy link
    Member

    pitrou commented Apr 9, 2018

    I pushed a fix for this in 3.8. Since the fix is a bit delicate, I'd rather not backport it. Thank you for reporting this issue!

    @pitrou pitrou closed this as completed Apr 9, 2018
    @serhiy-storchaka
    Copy link
    Member

    This change looks safe to me. I was just not sure that it is enough for practical cases. What if after allocating a large buffer the rest of the new area would be used for allocating small buffers? They can keep references to the large area after freeing the large buffer. Perhaps it is worth to block marking the remainder of a large area available. This will increase the memory consumption by small percent, but will reduce the risk of prolonging the life time of large blocks.

    @pitrou
    Copy link
    Member

    pitrou commented Apr 9, 2018

    Yes, it's a minimal effort. More sophisticated behavior would require a more sophisticated allocator.

    @vstinner
    Copy link
    Member

    New changeset 857d573 by Victor Stinner in branch '3.7':
    bpo-39932: Fix multiprocessing test_heap() (GH-19690)
    857d573

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes OS-windows performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants