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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:31:57+00:00 2026-05-28T00:31:57+00:00

How can I best determine the Size of a Class from Memory? Here is

  • 0

How can I best determine the Size of a Class from Memory?

Here is a basic sample class to work with – Note the variables serve no purpose other than for the example:

type
  TMyClass = class
  public
    fString1: string;
    fString2: string;
    fInteger1: Integer;
    fInteger2: Integer;

    constructor Create;
    destructor Destroy; override;
  end;

What I would like to do is return the same size as if it were a file on disk.

So if I saved TMyClass to File and the Size of the File was 2.4kb for example, I would like to get that value without even needing the File to be on disk (getting the size from Memory).

I have been searching and reading before asking here, this is what I have tried so far with mixed results:

  • InstanceSize

    Using TMyClass.InstanceSize on my class only ever returns the value 12.

  • SizeOf

    Using SizeOf(TMyClass) on my class only ever returns the value 4.

Whatever the values return I will be formatting to FileSize format such as kilobytes and megabytes etc.

I must be doing something wrong because I know for a fact there is more than 12 (or 4) bytes of data being used on my class. I also know I am referencing the correct class, it is the same class I am creating and freeing in the Form Create and Destroy events – as well as using at runtime.

Is what I am trying to do the correct way to go about it, and if so should my examples work? If they should work, I know then I need to look closer at my actual classes and objects.

The dirty way to achieve this would be for me to save my object classes to file, read the file size and then delete the temp file. But I don’t like doing approaches like that, I think they are dirty and taking shortcuts. The purpose is to read the size of my classes from memory, without resorting to saving and temp files etc.

I look forward to hearing your advice and suggestions thanks.

  • 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-28T00:31:58+00:00Added an answer on May 28, 2026 at 12:31 am

    Not sure how you got 12 for your InstanceSize on a class like that. It should be 20 (pre-D2009) or 24 (D2009 and later). The reason it’s so much smaller than the actual size of your saved file is because the strings aren’t held in the object itself; they’re reference types, implemented as pointers to the actual string data.

    Your “dirty approach” is on the right track. Pretty much the only way to find out how much disk space you need for an object like that is to actually serialize it. But you don’t need to save it to disk. If you’re currently saving the object with a TFileStream, (and you should be if you’re not,) use a TMemoryStream instead, which will “save” it to a memory buffer. Then get the Size of the stream and that’s the serialized size of your object, all without having to create and then delete a temp file.

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

Sidebar

Related Questions

I can't best describe this in words, so I'll show you with pictures. Here's
Possible Duplicate: In Java, what is the best way to determine the size of
Looking at a query's EXPLAIN plan, how does one determine where optimisations can best
How can I associate posts with pages? Trying to determine the best architecture /
Given var x, what is the best way to determine if x can have
How can one best test a controller action which receives a file upload using
How can I best implement a system for send keystrokes/commands via an iPhone and/or
There are two questions to this: How can I best spread the word about
Any ideas of how best i can implement article revision history for a Java
I'll try to explain this the best I can. I have a drop down

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.