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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:42:43+00:00 2026-06-11T01:42:43+00:00

I have a data structure, like this: public class Data{ private String charData; private

  • 0

I have a data structure, like this:

public class Data{
  private String charData;
  private int intData;

  //get-set methods here
}

Now, I want to create a lru cache and I need to override sizeof…as each char in java occupies 2bytes, while an int is 4 bytes I though making:

cachedData= new LruCache<String,Data>(CACHE_MB*1024*1024){
    protected int sizeOf(String k,Data v){ 
                 return 4 +2*v.getCharData().length();
 }

but Strings are objects, so I think they take more than just memory for characters, moreover Data is an object too so I’m not sure my method is correct.

By the way what happens if I reach maximum cache size with a wrong sizeOf method?

  • 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-11T01:42:44+00:00Added an answer on June 11, 2026 at 1:42 am

    I think you did not understand the use of the sizeOf() method:

    Returns the size of the entry for key and value in user-defined units.
    The default implementation returns 1 so that size is the number of
    entries and max size is the maximum number of entries.

    This method can be overridden in specific cases where the size of certain entries store in the cache are significantly larger than others (this can happen when caching Bitmaps). In this way, you can specify the maximum limit retain by the cache in terms of your unit of size rather than by the number of entries. Once this limit is reached, the Least Recently Used entries are evicted.

    In your case it’s not required to override it, unless the String object is going to store extremely long strings.

    If you insist on overriding it, you may do so like this:

    protected int sizeOf(String k, Data v) {
        return v.charData.length();
    }
    

    As the documentation says, the returned size can be an any user-defined unit. So the String.length() would do fine. Since the size of the int is insignificant compared to the long Strings, I’ve left it out.

    Basically, you need to return a number that represents a relative size of an entry.

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

Sidebar

Related Questions

i have a data structure like this public class Employee { public string Name
I have this data structure: class Conference { private List<List<string>>_orgs; public List<List<string>> Orgs {
My table have data structure like this cate_id task_id date_start date_end other 34 14
Using EF4 and Linq I have a data structure which looks like this: parentid
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have data from a CD or Record currently setup like this: class Release
I'm a bit confused on this. I have a data table structured like this:
I have a JavaScript data structure like the following in my Node.js/Express web app:
I have a JSON-like data structure (which I don't want to change) that is

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.