I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 27 14:42:19 PST 2015


On 28/01/2015 11:39 a.m., Gan wrote:
> On Tuesday, 27 January 2015 at 22:30:13 UTC, Gan wrote:
>> On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole wrote:
>>> On 28/01/2015 9:59 a.m., Gan wrote:
>>>> On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:
>>>>> On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:
>>>>>> Gan:
>>>>>>
>>>>>>> Is there some special stuff I gotta do extra with structs? Do they
>>>>>>> need manually allocated and released?
>>>>>>
>>>>>> Most of your usages of tiny structs should be by value. So just keep
>>>>>> in mind they are values. Even when you iterate with a foreach on a
>>>>>> mutable array of them :-)
>>>>>>
>>>>>>
>>>>>>> On a second question, do I ever need to manually release objects I
>>>>>>> create with new?
>>>>>>
>>>>>> Usually not. How much advanced do you want to be? :-)
>>>>>>
>>>>>> Bye,
>>>>>> bearophile
>>>>>
>>>>> Thanks. I'll give structs a try.
>>>>>
>>>>> When I start the program, it runs fine at 35mb of ram. It only keeps
>>>>> 15 objects stored in the arrays at a time so why do you think my ram
>>>>> usage increases to 700+ after many hours?
>>>>
>>>> Curiously, my CPU usage went from 10% to 5% after I changed to structs
>>>> on Point and Range. Though my memory still climbs high.
>>>
>>> Force a GC.collect() now and again. Disable it at the beginning too.
>>
>> I did a test and ran GC.collect() every loop but my memory usage
>> continues to rise. I can see how you'd be able to lower CPU usage by
>> running GC.collect() every now and then but right now I'm stuck on the
>> memory issue.
>>
>> Perhaps my problem lies in the C++ library SFML?
>
> I commented out some stuff and it appears my massive memory consumption
> comes from my tile.redraw function:
>
> void redraw() {
>          undrawn = false;
>          canvas.clear();
>
>          //Add stars
>          for (int i = 0; i < controller.starsPerTile; i++) {
>              Random gen;
>              gen.seed(unpredictableSeed);
>              int x = uniform(0, controller.tileSize, gen);
>              int y = uniform(0, controller.tileSize, gen);
>              double s = uniform(0, 10000, gen) / 10000.0;
>              double size = (s * (controller.starSizeMax -
> controller.starSizeMin)) + controller.starSizeMin;
>              if (x - size / 2 < 0) {
>                  x += size / 2;
>              }
>              if (y - size / 2 < 0) {
>                  y += size / 2;
>              }
>              if (x + size / 2 > controller.tileSize) {
>                  x -= size / 2;
>              }
>              if (y + size / 2 > controller.tileSize) {
>                  y -= size / 2;
>              }
>              drawStar(canvas, x, y, size, size);
>          }
>
>          canvas.display();
>      }
>      void drawStar(RenderTarget target, int centerX, int centerY, double
> width, double height) {
>          CircleShape star;
>          if (controller.multiColor == false) {
>              star = controller.stars[0];
>          } else {
>              Random gen;
>              gen.seed(unpredictableSeed);
>              star = controller.stars[uniform(0, controller.stars.length
> - 1, gen)];
>          }
>          star.position = Vector2f(centerX, centerY);
>          star.origin = Vector2f(0.5, 0.5);
>          star.scale = Vector2f(width / 100.0, height / 100.0);
>          target.draw(star);
>      }
>
>
> Would you know why this is using hundreds of mb of rams?

Try with only drawStart stripped.
If it still does it, then its redraw. Also try with just a hard coded 
call to drawStart.
Something smells wrong here.


More information about the Digitalmars-d-learn mailing list