4 Replies - 1797 Views - Last Post: 29 September 2012 - 02:37 AM Rate Topic: -----

#1 triplesowcow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 31-August 11

How to use less memory with a catalog?

Posted 28 September 2012 - 01:58 PM

I am a beginning programmer trying to create as many programs as I can to learn more about coding. My latest project is a photo album.

My problem is that with 100-150 medium resolution photos loaded into the program I am using over a gig of ram.

My program is structured as a main form with the gui to interact with an object I called archive, which contains simply a list of objects called albums, which has a string called name and a list of photo objects, which have a string name, a string caption and an image object.

The user opens a single album and all the photos inside are displayed in the form with controls to do basic things in like rotate, change the caption and name, move the photo from album to album and so on.

What I would like is a way to "unload" all of the other albums from the ram when the user selects the album they want to look at, but still be able to load them back up when they click on them in the album selection box.

How would I go about doing this?

Is This A Good Question/Topic? 0
  • +

Replies To: How to use less memory with a catalog?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: How to use less memory with a catalog?

Posted 28 September 2012 - 02:38 PM

There is no 'special trick' for this. Memory is returned when Garbage Collection takes place on all objects that go out of scope.

If you have your variables (collections) defined to be accessible program-wide then they never go out of scope - so you don't get that memory back.

First off, if you are loading and keeping in memory the full size image then you are doing this to yourself. You have to work smarter not harder. Loading at 8mb image to show a 800x600 image on screen is a huge waste of ram and putting a large burden on the OS because you are relying on it to shrink the image to fit the picture box. Instead, make your own thumbnail to match the size of the picturebox. Now you are using 3k instead of 8m.

As I said, no magic tricks here. Just a good understanding of proper design. You would see the same memory usage if you loaded 100-150 images into Photoshop and had them all open at the same time. So the only trick is to not load 100-150 full size images at one time.
Was This Post Helpful? 1
  • +
  • -

#3 triplesowcow  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 31-August 11

Re: How to use less memory with a catalog?

Posted 28 September 2012 - 05:00 PM

I have the full images loaded because when the user clicks a photo in the catalog, they will open in full screen at full resolution. My original goal for the programs was for organizing and cataloging the photos for later use, the photos need to be in full detail so they can be edited and manipulated later. What I was hoping for was a a way to have a "file reference object", is there really no way to have a permanent 'link' or reference to a file on the users hard drive?

This post has been edited by tlhIn`toq: 28 September 2012 - 05:29 PM
Reason for edit:: No need to quote the entire previous message. We all see it.

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: How to use less memory with a catalog?

Posted 28 September 2012 - 06:11 PM

View Posttriplesowcow, on 28 September 2012 - 06:00 PM, said:

I have the full images loaded because when the user clicks a photo in the catalog, they will open in full screen at full resolution.

Fine. Then at that time when the user clicks a thumbnail load the full size image. When they are done with it and it closes it should go out of scope and GC will {eventually} recover the memory. Front-loading ALL the photos is like saying "Over the course of my life I'll use 50,000 gallons of milk, so I'll buy them all now and store them just so I don't have to go to the store a second time. Anyone know how I can store 50,000 gallons of milk in a 1 bedroom apartment?"

View Posttriplesowcow, on 28 September 2012 - 06:00 PM, said:

My original goal for the programs was for organizing and cataloging the photos for later use, the photos need to be in full detail so they can be edited and manipulated later.

If your goal is to manipulate pictures, there are a dozen programs you can download for free.
If your goal is to learn and educate yourself about coding this is a decent project.

View Posttriplesowcow, on 28 September 2012 - 06:00 PM, said:

What I was hoping for was a a way to have a "file reference object", is there really no way to have a permanent 'link' or reference to a file on the users hard drive?

Well of course there is. Its just a path to the file. Its a string. Are you looking for some other data type? I'm not sure I'm understanding the exact problem or what it is you are looking for.

This post has been edited by tlhIn`toq: 28 September 2012 - 06:11 PM

Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,634
  • Joined: 16-October 07

Re: How to use less memory with a catalog?

Posted 29 September 2012 - 02:37 AM

So, your Photo object loads the image into memory no matter what? Clearly not the best plan. You have to ask yourself when and where do you need that image data. Your form will want the actual image, but the rest of your program wont. Photo, rather than just holding an image, would have a method to retrieve an image as requested.

There are a number of design principals you'll find: late binding, lazy initialization, lazy loading. They all address the same concern; only allocate resources when you must. If you feel your Photo class needs to hold an image, don't load on creation, only on request.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1