c# - ESENT数据库内存泄漏
本文关键字:内存 泄漏 数据库 ESENT | 更新日期: 2023-09-27 18:11:24
我刚刚开始使用ESENT ManagedInterface (http://managedesent.codeplex.com/)。我想知道它是否有内存泄漏问题。
我正在做的事情相当简单。我遵循样例代码,但是我在每一行中放入了一个相当大的字符串数据(10KB+),总共生成了10000行。
当插入更多行时,内存使用增加。如果我插入100,000行,程序将消耗1gb内存并死亡。
代码如下
public static void test()
{
string techcrunchString = @"The Latest from TechCrunch CMU Researchers Turn Any Surface Into A TouchscreenWeb Design Community Treehouse Raises $600K From Reid Hoffman, Kevin Rose, And Others Greylock Looks To Help Portfolio Companies Recruit Talent With New Hires UberMedia Quietly (Inadvertently?) Releases Chime.in, A Mobile Social Networking App T-Mobile Announces The Dual-Screen LG DoublePlay, Launching November 2nd? Watch An iPhone 4S and Samsung Galaxy S II Take Three Nasty Drops Onto Concrete Facebook, NRDC & Opower To Partner On Energy-Saving Social AppCTIAs New Alert Guidelines Could Mean The End Of Bill ShockGrockit Gets A $7 Million Venture Infusion And Launches Video Q&A Site Grockit AnswersGorgeous Photos, Tablet Browsing: 500px Debuts New iPad AppSamsung Galaxy Nexus, HTC Vigor To Launch November 10?Freelance.com: Facebook App, 3D, HTML5, And Cocoa Jobs On The RiseiPhone 4S First Weekend Sales Exceeds 4 Million, Doubles The Pace Of The iPhone 4Wahanda Secures $5.5 Million From Fidelity Growth Partners EuropeLook Out Uber: GroundLink Launches An Affordable, Mobile Private Car Service For New YorkersVideo Collaboration Software Maker ViVu Acquired By PolycomWith 400,000 Users Under Its Belt, SohoOS Plans Major Revamp5 Product Innovations From CEATEC 2011 In Japan (Video Gallery)Digital Media Companies Inuvo And Vertro To MergeRIM Apologizes With Free Apps & Technical Support For Three Days Of DowntimeCMU Researchers Turn Any Surface Into A TouchscreenPosted: 17 Oct 2011 09:14 AM PDT";
JET_INSTANCE instance;
JET_SESID sesid;
JET_DBID dbid;
JET_TABLEID tableid;
JET_COLUMNDEF columndef = new JET_COLUMNDEF();
// Initialize ESENT. Setting JET_param.CircularLog to 1 means ESENT will automatically
// delete unneeded logfiles. JetInit will inspect the logfiles to see if the last
// shutdown was clean. If it wasn't (e.g. the application crashed) recovery will be
// run automatically bringing the database to a consistent state.
Api.JetCreateInstance(out instance, "instance");
Api.JetSetSystemParameter(instance, JET_SESID.Nil, JET_param.CircularLog, 1, null);
Api.JetInit(ref instance);
Api.JetBeginSession(instance, out sesid, null, null);
// Create the database. To open an existing database use the JetAttachDatabase and
// JetOpenDatabase APIs.
Api.JetCreateDatabase(sesid, "edbtest.db", null, out dbid, CreateDatabaseGrbit.OverwriteExisting);
// Create the table. Meta-data operations are transacted and can be performed concurrently.
// For example, one session can add a column to a table while another session is reading
// or updating records in the same table.
// This table has no indexes defined, so it will use the default sequential index. Indexes
// can be defined with the JetCreateIndex API.
Api.JetBeginTransaction(sesid);
Api.JetCreateTable(sesid, dbid, "table", 0, 100, out tableid);
JET_COLUMNID id;
columndef.coltyp = JET_coltyp.Binary;
columndef.cp = JET_CP.ASCII;
Api.JetAddColumn(sesid, tableid, "id", columndef, null, 0, out id);
JET_COLUMNID blob;
columndef.coltyp = JET_coltyp.LongBinary;
//columndef.cp = JET_CP.ASCII;
Api.JetAddColumn(sesid, tableid, "blob", columndef, null, 0, out blob);
string indexDef = "+id'0'0";
Api.JetCreateIndex(sesid, tableid, "primary", CreateIndexGrbit.IndexPrimary, indexDef, indexDef.Length, 100);
//Api.JetSetCurrentIndex(sesid, tableid, null);
Api.JetCommitTransaction(sesid, CommitTransactionGrbit.LazyFlush);
long Process_MemoryStart = 0;
Process MyProcess = System.Diagnostics.Process.GetCurrentProcess();
Process_MemoryStart = MyProcess.PrivateMemorySize64;
Console.WriteLine("Before loop : " + Process_MemoryStart / 1024 + "KB");
int i = 0;
for (int t = 0; t < 20; t++)
{
Api.JetBeginTransaction(sesid);
for (int j = 0; j < 500; j++)
{
i = t * 500 + j;
string dataString = techcrunchString + i.ToString();
byte[] data = Encoding.UTF8.GetBytes(dataString);
string keyString = i.ToString();
byte[] key = Encoding.UTF8.GetBytes(keyString);
//store
Api.MakeKey(sesid, tableid, key, MakeKeyGrbit.NewKey);
bool exists = Api.TrySeek(sesid, tableid, SeekGrbit.SeekEQ);
if (exists)
{
Api.JetPrepareUpdate(sesid, tableid, JET_prep.ReplaceNoLock);
//Console.WriteLine("store: " + "update");
}
else
{
Api.JetPrepareUpdate(sesid, tableid, JET_prep.Insert);
Api.SetColumn(sesid, tableid, id, key);
//Console.WriteLine("store: " + "insert");
}
Api.SetColumn(sesid, tableid, blob, data);
Api.JetUpdate(sesid, tableid);
if (i % 500 == 0)
{
long Process_MemoryStart1 = 0;
Process MyProcess1 = System.Diagnostics.Process.GetCurrentProcess();
Process_MemoryStart1 = MyProcess1.PrivateMemorySize64;
Console.WriteLine("Finished " + i.ToString() + " : " + Process_MemoryStart1 / 1024 + "KB");
}
}
Api.JetCommitTransaction(sesid, CommitTransactionGrbit.None);
}
Process_MemoryStart = 0;
MyProcess = System.Diagnostics.Process.GetCurrentProcess();
Process_MemoryStart = MyProcess.PrivateMemorySize64;
Console.WriteLine("Loop finished: " + Process_MemoryStart / 1024 + "KB");
// Terminate ESENT. This performs a clean shutdown.
Api.JetCloseTable(sesid, tableid);
Process_MemoryStart = 0;
MyProcess = System.Diagnostics.Process.GetCurrentProcess();
Process_MemoryStart = MyProcess.PrivateMemorySize64;
Console.WriteLine("After close table: " + Process_MemoryStart / 1024 + "KB");
Api.JetEndSession(sesid, EndSessionGrbit.None);
Process_MemoryStart = 0;
MyProcess = System.Diagnostics.Process.GetCurrentProcess();
Process_MemoryStart = MyProcess.PrivateMemorySize64;
Console.WriteLine("After end session: " + Process_MemoryStart / 1024 + "KB");
Api.JetTerm(instance);
Process_MemoryStart = 0;
MyProcess = System.Diagnostics.Process.GetCurrentProcess();
Process_MemoryStart = MyProcess.PrivateMemorySize64;
Console.WriteLine("After term instance: " + Process_MemoryStart / 1024 + "KB");
}
在上面的代码中,它上升到大约100 MB。只有当我执行Api.JetTerm(instance)时,内存才被释放。
在我的实际问题中,我必须不断地插入大量的数据行很多很多次,所以它不会以这种方式为我工作,因为内存最终会被吃掉。
有谁能帮我一下吗?**为什么即使提交了事务,event也会占用内存?
我怀疑这是撤销的事情内部持有内存,如果它是,如何关闭它?我不需要撤销。* *
感谢p。S:我已经在两个32位&64位Windows,都有相同的内存问题
这有什么帮助吗:http://www.nikosbaxevanis.com/bonus-bits/2010/10/adventures-using-rhino-servicebus.html ?
Microsoft.Isam.Esent.Interop。JET_param, CacheSizeMax该参数配置数据库页缓存的最大大小。尺寸是中码数据库页面。如果此参数保留其默认值,则缓存的最大大小将被设置为物理的大小调用JetInit时的内存。
设置Microsoft.Isam.Esent.Interop.SystemParameters.CacheSizeMax到1024或512似乎可以解决内存使用日益增加的问题。
抱歉我来晚了…
我注意到这样做并不是修复内存泄漏,而是降低了引擎的性能。缓存大小是在内存中缓存数据库页面,这样就可以减少对磁盘的访问…就像一个好的数据库应该做的那样。;) 如果你降低了这个值,你就会降低性能,并且需要更多的磁盘IO。
数据库引擎很聪明,当它看到其他人使用内存时,它会后退,所以理论上你不应该有这样的缓存大小,如果它看到其他人需要它,它会释放内存。
所以,除非你真的知道你需要这样做,否则我不建议你这么做