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 6190785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:39:05+00:00 2026-05-24T02:39:05+00:00

I was wondering if someone could explain the best solution for the smallest memory

  • 0

I was wondering if someone could explain the best solution for the smallest memory footprint for an object that has a file in the following situation…

  1. There could be 1 to a few hundred Foo classes.
  2. Thread safety will be important down the road.
  3. Not every Foo class’s file is accessed every time.
  4. Each file is unique.
  5. The file in a Foo class may be accessed more than once.

I was planning to profile the solutions below to find the lowest memory footprint and i have a good idea which one would work best but I was interested in some feedback. Solution 1 seems like the best approach but it feels prone to memory leaks the more something accesses the getter. Thoughts?

Solution 1:

public class Foo{
        private final String pathToFile;

        public class Foo(String pathToFile){
            this.pathToFile = pathToFile;
        }

        public File getFile(){ 
            return new File(pathToFile);
        }
}

Solution 2:

 public class Foo{
        private final File file;

        public class Foo(String pathToFile){
            this.file = new File(pathToFile);
        }

        public File getFile(){ 
            return file;
        }
    }

Solution 3:

public class Foo{
        private final String pathToFile;
        private File file = null;

        public class Foo(String pathToFile){
            this.pathToFile = pathToFile;
        }

        public File getFile(){ 
            if (file == null){
                file = new File(pathToFile);
            }

            return file;
        }
}
  • 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-05-24T02:39:05+00:00Added an answer on May 24, 2026 at 2:39 am

    It all depens on what you want to do with the program, If you need the path in other places then you should have a reference to that. if you need the file, again you would need a reference. Another solution you could do is in the second solution have a method that will return the path: file.getPath();

    So overall either the first solution (if you need the path at some point),
    or solution 2 if you do not.

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

Sidebar

Related Questions

I was wondering if someone could explain how the following code works: public interface
I ran across this and was wondering if someone could explain why this works
I saw this tip in another question and was wondering if someone could explain
Wondering if someone with experience could possibly explain this a bit more. I have
I was wondering if someone could explain what Func<int, string> is and how it
I was wondering if someone could explain in detail what (int)(l ^ (l >>>
I was wondering if someone could explain to me what the #pragma pack preprocessor
I am very confused over something and was wondering if someone could explain. In
I'm wondering if someone could explain why the retain count of a newly created
I was wondering if someone could explain in layman's terms what partial ordering of

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.