是否以与文件大小相同的格式获取类或对象的大小? [英] Getting the Size of a Class or Object in the same format as a File Size?

查看:55
本文介绍了是否以与文件大小相同的格式获取类或对象的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何最好地从内存确定类的大小?

这是要使用的基本示例类-请注意,变量仅用于示例目的,

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.

因此,如果我将TMyClass保存到文件中,并且文件的大小为2.4kb,那么我甚至不需要将文件放在磁盘上就可以获取该值(从内存中获取大小).

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

在我的课程上使用TMyClass.InstanceSize只会返回值12.

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

SizeOf

在我的课程上使用SizeOf(TMyClass)仅返回值4.

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

无论返回什么值,我都将格式化为FileSize格式,例如千字节和兆字节等.

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

我肯定做错了,因为我知道一个事实,就是我的类中使用了超过12(或4)个字节的数据.我也知道我引用的是正确的类,它是我在Form Create和Destroy事件中创建和释放的类,以及在运行时使用的类.

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.

推荐答案

不确定在类似的类上,如何为您的 InstanceSize 获得12.它应该是20(D2009之前)或24(D2009及更高版本).它之所以比已保存文件的实际大小小得多的原因是,字符串没有保存在对象本身中;而是在对象本身中保留了字符串.它们是引用类型,实现为指向实际字符串数据的指针.

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.

您的肮脏方法"走在正确的轨道上.找出这样的对象需要多少磁盘空间的唯一方法就是实际对其进行序列化.但是您无需将其保存到磁盘.如果当前正在使用TFileStream保存对象(如果不是,则应该这样做),请改用TMemoryStream,它将保存"到内存缓冲区.然后获取流的 Size ,这就是对象的序列化大小,而无需创建然后删除临时文件.

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.

这篇关于是否以与文件大小相同的格式获取类或对象的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆