简体   繁体   中英

Usercontrol uses an excessive amount of memory when added to a control, eventually get Out of Memory error

I have a program that loads movie information from a db with images and other information (such as overview text etc). As a test I'm having it load about 480 movies of 1200+.

My MovieTile control contains a picture box and tooltip control on it, and has about 10 public string properties.

I have a list of the usercontrol List<MovieTile>(); . And after loading all the movies and data etc, and Initializing the tile, my program takes about 12mbs.

Once I have it add the movie tiles to the FlowLayoutPanel the memory jumps to 1.4GBs. Once I start scrolling through the panel to start viewing the tiles, after getting about half way through I of course to no surprise get an "Out of memory" exception. All from uncommenting pnlMovies.Controls.Add(tile);

I tried manually adding the tiles to a panel, thinking maybe the flowlayoutpanel has some overhead or something with so many controls inside of it, but I ran into the same problem.

Is there another way around this issue or what could possibly be causing the huge spike in memory? Or maybe there is a more efficient and proper way of dealing with so many controls?

Thanks in advance.

Instead of having all your items loaded at the same time, I suggest to develop or use a container control that can handle virtual mode and paging.

By this way, you can load and display only the items that are shown. So the memory consumption will not exceed the amount required by displayed items.

I suggest you to try the ListView . It has a Virtual Mode that is easy to use.

If your user control is highly customized and does more than displaying a picture, then the ListView might not be the right solution. Then you can look at the DataRepeater control which implements a VirtualMode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM