我需要在处理大型数组时节省物理内存。在我当前的代码版本中,我为每个数据类型定义了一个类似
的类Table2
我有~50种不同的基本类类型和这些类的几种组合。
using (var context = new ctxdB())
{
var tbl1 = context.Table1.FirstOrDefault(c => c.Token == token);
var tbl2 = context.Table2.Find(id);
tbl1.IsMale = true;
tbl2.Name = "New name";
context.Entry(tbl1).State = EntityState.Modified;
context.Entry(tbl2).State = EntityState.Modified;
context.SaveChanges();
}
在当前的代码版本中,我可以读取和写入这些类的类型,并将所有这些类的列表作为列表/数组中的项目运行。
但是现在我因内存问题而无法运行,因为我的方法无效 因为每个类类型消耗16个字节用于访问+额外的字节用于数据本身。
此外,如果在组合类中,并非所有子类都填充了数据 我用这个静态的方法创建了许多不经常使用的内存空间。
Delphi中存储更好的方法来存储具有较低内存占用量的不同数据吗?
答案 0 :(得分:2)
你可以这样做(如果内存使用率低于代码简单性):
const
MaxBufferSize = 16; // the largest size you actually need, in this example sizeof(TMyData_04)...
type
TMyData = packed record
TypeID: Byte;
Buffer: array[0..MaxBufferSize-1] of Byte;
end;
PMyData_01 = ^TMyData_01;
TMyData_01 = packed record
Data: Integer;
end;
PMyData_02 = ^TMyData_02;
TMyData_02 = packed record
Data: String;
end;
PMyData_03 = ^TMyData_03;
TMyData_03 = packed record
Data: TDateTime;
Index: Integer;
end;
PMyData_04 = ^TMyData_04;
TMyData_04 = packed record
Data: TDateTime;
Value: Real;
end;
...
然后,您可以将TMyData
个实例的数组分配到所需的长度,并调用System.Initialize()
来初始化包含编译器管理的数据类型的任何项:
var
Arr: array of TMyData;
SetLength(Arr, ...);
//...
Arr[Index].TypeID := $01;
System.Initialize(PMyData_01(@Arr[Index].Buffer)^);
// populate PMyData_01(@Arr[Index].Buffer)^ fields as needed...
// and so on ...
在解除分配数组之前,不要忘记在项目上调用System.Finalize()
,以避免任何内存泄漏:
var
I: Integer;
begin
for I := Low(Arr) to High(Arr) do
begin
case Arr[I].TypeID of
$01: System.Finalize(PMyData_01(@Arr[i].Buffer)^);
$02: System.Finalize(PMyData_02(@Arr[i].Buffer)^);
$03: System.Finalize(PMyData_03(@Arr[i].Buffer)^);
$04: System.Finalize(PMyData_04(@Arr[i].Buffer)^);
// and so on ...
end;
end;
end;
不理想,但功能正常......
通过一些Generics技巧,您可以稍微清理一下代码:
const
MaxBufferSize = 16; // the largest size you actually need, in this example sizeof(TMyData_04)...
type
TMyDataHelper<T: record> = record
type PtrType = ^T;
class function GetDataTypeID: Byte; static;
end;
TMyData = packed record
TypeID: Byte;
Buffer: array[0..MaxBufferSize-1] of Byte;
procedure InitializeBuffer<T: record>;
procedure FinalizeBuffer;
procedure SetBufferData<T: record>(const NewData: T);
function BufferAs<T: record>: TMyDataHelper<T>.PtrType;
end;
TMyData_01 = packed record
Data: Integer;
end;
TMyData_02 = packed record
Data: String;
end;
TMyData_03 = packed record
Data: TDateTime;
Index: Integer;
end;
TMyData_04 = packed record
Data: TDateTime;
Value: Real;
end;
// and so on ...
class function TMyDataHelper<T>.GetDataTypeID: Byte;
begin
if TypeInfo(T) = TypeInfo(TMyData_01) then
Result := $01
else
if TypeInfo(T) = TypeInfo(TMyData_02) then
Result := $02
else
if TypeInfo(T) = TypeInfo(TMyData_03) then
Result := $03
else
if TypeInfo(T) = TypeInfo(TMyData_04) then
Result := $04
// and so on ...
else
Result := $00;
end;
procedure TMyData.InitializeBuffer<T>;
var
LTypeID: Byte;
begin
LTypeID := TMyDataHelper<T>.GetDataTypeID;
if TypeID <> LTypeID then
begin
FinalizeBuffer;
System.Initialize(BufferAs<T>^);
TypeID := LTypeID;
end;
end;
procedure TMyData.FinalizeBuffer;
begin
case TypeID of
$01: Finalize(BufferAs<TMyData_01>^);
$02: Finalize(BufferAs<TMyData_02>^);
$03: Finalize(BufferAs<TMyData_03>^);
$04: Finalize(BufferAs<TMyData_04>^);
// and so on ...
else
FillChar(Buffer, SizeOf(Buffer), $00);
end;
end;
procedure TMyData.SetBufferData<T>(const NewData: T);
begin
InitializeBuffer<T>;
BufferAs<T>^ := NewData;
end;
function TMyData.BufferAs<T>: TMyDataHelper<T>.PtrType;
begin
Result := TMyDataHelper<T>.PtrType(@Buffer);
end;
var
Arr: array of TMyData;
SetLength(Arr, ...);
FillChar(Arr[0], Length(Arr)*SizeOf(TMyData), $0);
//...
Arr[Index].InitializeBuffer<TMyData_01>;
populate Arr[Index].BufferAs<TMyData_01>^ fields as needed...
or:
Arr[Index].SetBufferData<TMyData_01>(...);
// and so on ...
var
I: Integer;
begin
for I := Low(Arr) to High(Arr) do
Arr[I].FinalizeBuffer;
end;