{"action":"created","issue":{"url":"https://api.github.com/repos/vispy/vispy/issues/724","labels_url":"https://api.github.com/repos/vispy/vispy/issues/724/labels{/name}","comments_url":"https://api.github.com/repos/vispy/vispy/issues/724/comments","events_url":"https://api.github.com/repos/vispy/vispy/issues/724/events","html_url":"https://github.com/vispy/vispy/issues/724","id":57713930,"number":724,"title":"memory continues increasing when rendering markervisuals to fbo","user":{"login":"ZuchaoWang","id":7141094,"avatar_url":"https://avatars.githubusercontent.com/u/7141094?v=3","gravatar_id":"","url":"https://api.github.com/users/ZuchaoWang","html_url":"https://github.com/ZuchaoWang","followers_url":"https://api.github.com/users/ZuchaoWang/followers","following_url":"https://api.github.com/users/ZuchaoWang/following{/other_user}","gists_url":"https://api.github.com/users/ZuchaoWang/gists{/gist_id}","starred_url":"https://api.github.com/users/ZuchaoWang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ZuchaoWang/subscriptions","organizations_url":"https://api.github.com/users/ZuchaoWang/orgs","repos_url":"https://api.github.com/users/ZuchaoWang/repos","events_url":"https://api.github.com/users/ZuchaoWang/events{/privacy}","received_events_url":"https://api.github.com/users/ZuchaoWang/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/vispy/vispy/labels/sprint-2015","name":"sprint-2015","color":"e11d21"},{"url":"https://api.github.com/repos/vispy/vispy/labels/type:%20bug","name":"type: bug","color":"fc2929"},{"url":"https://api.github.com/repos/vispy/vispy/labels/type:%20performance","name":"type: performance","color":"fcdb04"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":10,"created_at":"2015-02-15T01:42:01Z","updated_at":"2015-03-26T14:50:01Z","closed_at":"2015-03-26T14:50:01Z","body":"Hi all,\r\n\r\nIn the following test code, I try to render one million markervisuals into numpy arrays. I tried it 100 times. Each time I rebuilt the MarkerVisuals object, and I can see that the memory consumption increases drastically, quickly reaching >2G. It also takes over 700ms to render a frame, which I believe is too slow. Can anyone tell me how to stop the memory leak, and how to make the rendering faster?\r\n\r\nP.S. I have made this test in win7 and ubuntu 14.04 OS. Besides, I understand that I do not need to rebuilt the MarkerVisuals every time. After all, the positions of visuals are fixed. However, in my real development code, the positions can change. So I rebuild the visuals in my test code.\r\n\r\n\tfrom __future__ import print_function, division\r\n\timport numpy as np\r\n\r\n\tfrom vispy import app, gloo, scene\r\n\tfrom vispy.color import Color, ColorArray, get_colormap\r\n\tfrom vispy.visuals.shaders import ModularProgram, Function\r\n\tfrom vispy.visuals import Visual, MarkersVisual, transforms\r\n\r\n\tfrom memory_profiler import profile\r\n\timport time\r\n\r\n\t\t\t\r\n\tclass MyCanvas:\r\n\t    def __init__(self):\r\n\t        self._canvas = app.Canvas(show=False)\r\n\t        self._canvas.show()\r\n\t        #self._canvas.show(visible=False)\r\n\t        \r\n\t    def __del__(self):\r\n\t        self.close()\r\n\t        \r\n\t    def render(self, image_size, world_boundary, visuals, viewport):\r\n\t        \"\"\"\r\n\t        Renders a list of visuals to a numpy-array image, with OpenGL.\r\n\t        \r\n\t        image_size: (width. height)\r\n\t        world_boundary: (xmin, xmax, ymin, ymax)\r\n\t        visuals: (visual0, visual1, ...), each must be an instance of vispy.visuals.Visual\r\n\t        viewport: x, y, w, h; usually: [0,0]+image_size       \r\n\t        \"\"\"\r\n\t        tr = transforms.STTransform()\r\n\t        coord_world = [[world_boundary[0], world_boundary[2]],\r\n\t                       [world_boundary[1], world_boundary[3]]]\r\n\t        coord_render = [[0, 0], image_size]\r\n\t        tr.set_mapping(coord_world, coord_render)\r\n\r\n\t        # the following three lines of codes cost less than 0.2 ms\r\n\t        # so it is not necessary to keep rendertex and fbo when image_size remains\r\n\t        # every time we just ask for new instances\r\n\t        rendertex = gloo.Texture2D(format='rgba',shape=(image_size[1],image_size[0],4))\r\n\t        fbo = gloo.FrameBuffer(rendertex)\r\n\t        self._canvas.size = image_size # this ensures tr_sys is correct\r\n\t        \r\n\t        im = None\r\n\t        with fbo:\r\n\t            gloo.set_viewport(*viewport)\r\n\t            gloo.clear()\r\n\t            tr_sys = transforms.TransformSystem(self._canvas)\r\n\t            tr_sys.visual_to_document = tr\r\n\t            for visual in visuals:\r\n\t                visual.draw(tr_sys)\r\n\t            im = fbo.read()\r\n\t            \r\n\t        return im\r\n\t    \r\n\t    def close(self):\r\n\t        self._canvas.close()\r\n\t\t\t\r\n\r\n\t@profile # this decorator print out the memory consumption\r\n\tdef run_test_once(mycanvas, pos):\r\n\t\t'''\r\n\t\tThis function is used for memory and cpu time test\r\n\t\tIt performs one pass of points rendering\r\n\t\t\r\n\t\tpos: the points to be render\r\n\t\tmycanvas: the MyCanvas instance used to render the points \r\n\t\t'''\r\n\t\tpoints = MarkersVisual()\r\n\t\tpoints.set_data(pos=pos)\r\n\t\t\t\t\t \r\n\t\timg_array = mycanvas.render(image_size=[600,600],\r\n\t                                world_boundary=[-1,1,-1,1],\r\n\t                                visuals=[points],\r\n\t                                viewport=[0,0,600,600])\r\n\t        \r\n\t\tdel points\r\n\t\t\r\n\t\treturn img_array\r\n\t\t\t\r\n\t\t\t\r\n\tif __name__ == '__main__':\r\n\t\tmycanvas = MyCanvas()\r\n\r\n\t\t# prepare a 2D point dataset with 1 million points\r\n\t\tn = 1000000\r\n\t\tpos = 0.25 * np.random.randn(n, 2).astype(np.float32)\r\n\t\t\r\n\t\t# run the test 100 times, to test the memeory consumption and cpu time usage\r\n\t\t# each time the 1 million points will be rendered once\r\n\t\tfor i in range(100):\r\n\t\t\tstart_time = time.time()\r\n\t\t\timg_array = run_test_once(mycanvas, pos)\r\n\t\t\tprint(\"--- %f seconds ---\" % (time.time() - start_time))\r\n\t\t\r\n\t\tmycanvas.close()"},"comment":{"url":"https://api.github.com/repos/vispy/vispy/issues/comments/86547926","html_url":"https://github.com/vispy/vispy/issues/724#issuecomment-86547926","issue_url":"https://api.github.com/repos/vispy/vispy/issues/724","id":86547926,"user":{"login":"Eric89GXL","id":2365790,"avatar_url":"https://avatars.githubusercontent.com/u/2365790?v=3","gravatar_id":"","url":"https://api.github.com/users/Eric89GXL","html_url":"https://github.com/Eric89GXL","followers_url":"https://api.github.com/users/Eric89GXL/followers","following_url":"https://api.github.com/users/Eric89GXL/following{/other_user}","gists_url":"https://api.github.com/users/Eric89GXL/gists{/gist_id}","starred_url":"https://api.github.com/users/Eric89GXL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Eric89GXL/subscriptions","organizations_url":"https://api.github.com/users/Eric89GXL/orgs","repos_url":"https://api.github.com/users/Eric89GXL/repos","events_url":"https://api.github.com/users/Eric89GXL/events{/privacy}","received_events_url":"https://api.github.com/users/Eric89GXL/received_events","type":"User","site_admin":false},"created_at":"2015-03-26T14:50:01Z","updated_at":"2015-03-26T14:50:01Z","body":"Appears to be fixed, feel free to reopen if you experience it again -- and thanks for the reports and test help!"}}