Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:37:38+00:00 2026-06-06T11:37:38+00:00

This question has baffled myself and my cohorts. In the program I had written

  • 0

This question has baffled myself and my cohorts. In the program I had written I was experiencing a memory leak. The var Platform, was being reassigned each iterations to a new object. But for some reason the old platform objects where not being cleaned by the gc and after many iterations the heap overflows:

Some of you may realise this is a PSO algorithm. But for those who don’t this function has to be evaluated 1000’s of times, and basicplatform is an extremely data extensive object, so multiple instances would eventually give a memory overflow, just to give a little context.

Buggy code:

public class Fitness implements FitnessFunction{

protected Platform platform;

public Fitness(){

}

public Fitness(Platform platform) {
    this.platform = platform;
}

@Override
public double fitness(Particle p) {

    try {
        platform = new BasicPlatform("testData.csv");
    } catch (Exception e) {
        e.printStackTrace();
    }





    platform.startSimulation();
    double prof = platform.getFitness();
    v.clear();
    if(prof != 0)
    return -prof;
    return 0;
}


}

After being confused as to why there is leak, as surely there shoudln’t be, my friend showed me this solution, which he used before in a similar situation:

public class TradingRuleFitness implements FitnessFunction{

protected Platform platform;

public Fitness(){

}

public Fitness(Platform platform) {
    this.platform = platform;
}

@Override
public double fitness(Particle p) {


    Vector<Platform> v = new Vector<Platform>();
    try {
        //platform = new BasicPlatform("testData.csv");
        v.add(new BasicPlatform("testData.csv"));
    } catch (Exception e) {
        e.printStackTrace();
    }



        double prof = v.get(0).getFitness;
        v.clear();
        if(prof != 0)
        return -prof;
        return 0;
    }


}

Nearly exactly the same but this time instead of re-assigning the var platform we create a new object inside a vector and delete it after I have finished with it.This method seems to force the gc into cleaning up.

The question is why does this vector method work but not the original which technically should and are there any cleaner solutions?

p.s I have cleaned up unnesscary bits of code, as the question is about the object creation and removal

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:37:39+00:00Added an answer on June 6, 2026 at 11:37 am

    In the first case, if your Fitness object si retained, so will your Platform object be retained as it a field.

    In the second case, your Platform is held in a local variable and is discard when fitness returns.

    It doesn’t have to be a Vector, it could be a plain local variable.

    Try removing the field in both examples and it should work either way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has two parts. Part 1. Yesterday I had some code which would
This question has developed off an answer here . My question therefore is what
This question has been asked before but i still don't understand it fully so
this question has probably been asked before, but i'm stuck and i've tried a
This question has disturbed me for a long time. Sorry if it is a
This question has been bugging me for a long time now but essentially I'm
This question has frequently been asked over stackoverflow.com but none of the answers work
This question has been asked in a C++ context but I'm curious about Java.
This question has been puzzling me for a long time now. I come from
Would be great if you guys could shed some light on this, has baffled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.